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
wip(memory): preserve bounded discovery worker checkpoint
Rahman8a2c0d19f2fd
diff --git a/app/api/v1/workflows/route.ts b/app/api/v1/workflows/route.tsindex cec23c46..1969f6d5 100644--- a/app/api/v1/workflows/route.ts+++ b/app/api/v1/workflows/route.ts@@ -1,3 +1,4 @@+import { discoverOwnerGraphs, cloneOwnerGraph } from "@/lib/workflow/owner-discovery";import { NextRequest, NextResponse } from "next/server";import { getSessionContext } from "@/lib/auth/require-session";import { roleAtLeast } from "@/lib/auth/roles";@@ -32,6 +33,7 @@ const fail=(error:unknown,status=400)=>NextResponse.json({error:error instanceofasync 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}`};}function definition(graph:NonNullable<Awaited<ReturnType<typeof getWorkflowGraph>>>){const{revision:_r,createdAt:_c,updatedAt:_u,version:_v,...rest}=graph;return rest;}export async function GET(req:NextRequest){const q=req.nextUrl.searchParams,minimum=(q.has("variables")||q.has("data_tables")||q.has("data_table"))?"operator":"viewer",session=await auth(minimum);if(!session)return fail("unauthorized",401);try{+ if(q.get("owner_view")==="1"){if(session.context.role!=="owner")return fail("owner_required",403);return NextResponse.json(await discoverOwnerGraphs(session.context.role,session.principal,Number(q.get("owner_offset"))||0),{headers});}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);const operator=roleAtLeast(session.context.role,"operator");@@ -55,6 +57,7 @@ export async function GET(req:NextRequest){const q=req.nextUrl.searchParams,miniconst graphs=await listWorkflowGraphs(session.principal),query=q.get("q")??"",filters={tag:q.get("tag")||undefined,status:q.get("status")||undefined,project:q.get("project")||undefined,folder:q.get("folder")||undefined,node:q.get("node")||undefined}; return NextResponse.json({graphs:graphs.filter((graph)=>workflowMatchesQuery(graph,query,filters))},{headers});}catch(error){return fail(error);}}export async function POST(req:NextRequest){const session=await auth("operator");if(!session)return fail("operator_required",403);try{const body=await readSetupJson(req),action=typeof body.action==="string"?body.action:"";if(rateLimited(`workflow:${action}:${session.context.session.device_id}`,action==="run"?60:30,60_000))return fail("rate_limited",429);+ if(action==="owner_clone"){if(session.context.role!=="owner")return fail("owner_required",403);return NextResponse.json({graph:await cloneOwnerGraph(session.context.role,session.principal,String(body.origin_owner??""),String(body.graph_id??""))},{headers});}if(action==="create")return NextResponse.json({graph:await createWorkflowGraph(session.principal,body.graph,"create",{remember:true})},{headers});if(action==="update")return NextResponse.json({graph:await updateWorkflowGraph(session.principal,String(body.graph_id??""),String(body.expected_revision??""),body.graph,"update",{remember:true})},{headers});if(action==="delete")return NextResponse.json(await deleteWorkflowGraph(session.principal,String(body.graph_id??""),String(body.expected_revision??"")),{headers});diff --git a/lib/contracts/workflow-discovery.ts b/lib/contracts/workflow-discovery.tsnew file mode 100644index 00000000..5fa8aa7a--- /dev/null+++ b/lib/contracts/workflow-discovery.ts@@ -0,0 +1,8 @@+export type WorkflowDiscoveryRow = {+ id: string; name: string; status: string; project?: string; updatedAt: string;+ originPrincipal: string; owner: string; readOnly: boolean; nodeCount: number;+};+export type WorkflowDiscoveryPage = {+ graphs: WorkflowDiscoveryRow[];+ scan: { ownerOffset: number; nextOwnerOffset?: number; ownersScanned: number; totalOwners: number; warnings: string[] };+};diff --git a/lib/workflow/graph-store.ts b/lib/workflow/graph-store.tsindex 990df908..e6ce0089 100644--- a/lib/workflow/graph-store.ts+++ b/lib/workflow/graph-store.ts@@ -35,7 +35,7 @@ function parseStoredGraph(raw: unknown): WorkflowGraph {return { ...definition, version: 2, id: row.id, createdAt: row.createdAt, updatedAt: row.updatedAt, revision: row.revision };}-async function readStore(owner: string, principal = ""): Promise<WorkflowGraphStore> {+export async function readWorkflowGraphStore(owner: string, principal = ""): Promise<WorkflowGraphStore> {const target = file(owner);try {const parsed = await readWorkflowJson(target, MAX_STORE_BYTES, "workflow graph store");@@ -72,7 +72,7 @@ function materialize(definition: WorkflowGraphDefinition, previous?: WorkflowGra}export async function listWorkflowGraphs(principal: string): Promise<WorkflowGraph[]> {- const owner = workflowGraphOwner(principal), store = await readStore(owner, principal);+ const owner = workflowGraphOwner(principal), store = await readWorkflowGraphStore(owner, principal);return store.graphs.slice().sort((a, b) => Date.parse(b.updatedAt) - Date.parse(a.updatedAt));}@@ -83,7 +83,7 @@ export async function getWorkflowGraph(principal: string, id: string): Promise<Wexport async function createWorkflowGraph(principal: string, raw: unknown, reason: WorkflowGraphVersionReason = "create", options: { remember?: boolean } = {}): Promise<WorkflowGraph> {const owner = workflowGraphOwner(principal), definition = parseWorkflowGraphDefinition(raw);const graph = await withGraphLock(owner, async () => {- const store = await readStore(owner, principal);+ const store = await readWorkflowGraphStore(owner, principal);if (store.graphs.length >= MAX_GRAPHS) throw new Error("private workflow graph limit reached");const graph = materialize({ ...definition, id: definition.id ?? randomUUID() });if (store.graphs.some((row) => row.id === graph.id)) throw new Error("workflow graph id already exists");@@ -96,7 +96,7 @@ export async function createWorkflowGraph(principal: string, raw: unknown, reasoexport async function updateWorkflowGraph(principal: string, id: string, expectedRevision: string, raw: unknown, reason: WorkflowGraphVersionReason = "update", options: { remember?: boolean } = {}): Promise<WorkflowGraph> {const owner = workflowGraphOwner(principal), definition = parseWorkflowGraphDefinition(raw);const graph = await withGraphLock(owner, async () => {- const store = await readStore(owner, principal), index = store.graphs.findIndex((graph) => graph.id === id);+ const store = await readWorkflowGraphStore(owner, principal), index = store.graphs.findIndex((graph) => graph.id === id);if (index < 0) throw new Error("workflow graph not found");const current = store.graphs[index];if (current.revision !== expectedRevision) throw new Error("workflow graph revision changed; refresh before editing");@@ -110,7 +110,7 @@ export async function updateWorkflowGraph(principal: string, id: string, expecteexport async function deleteWorkflowGraph(principal: string, id: string, expectedRevision: string): Promise<{ id: string; deleted: true }> {const owner = workflowGraphOwner(principal);return withGraphLock(owner, async () => {- const store = await readStore(owner, principal), current = store.graphs.find((graph) => graph.id === id);+ const store = await readWorkflowGraphStore(owner, principal), current = store.graphs.find((graph) => graph.id === id);if (!current) throw new Error("workflow graph not found");if (current.revision !== expectedRevision) throw new Error("workflow graph revision changed; refresh before deleting");store.principal = principal; store.graphs = store.graphs.filter((graph) => graph.id !== id); await writeStore(store); return { id, deleted: true };@@ -153,9 +153,9 @@ export async function ensureLearnedWorkflowGraph(recipe: LearnedRecipe): Promiseif (!recipe.successes || !recipe.bestSteps.length) return null;const principal = recipe.actor, fingerprint = learnedFingerprint(recipe), owner = workflowGraphOwner(principal);return withGraphLock(owner, async () => {- const store = await readStore(owner, recipe.actor), existing = store.graphs.find((graph) => graph.metadata.fingerprint === fingerprint);+ const store = await readWorkflowGraphStore(owner, recipe.actor), existing = store.graphs.find((graph) => graph.metadata.fingerprint === fingerprint);if (existing) return existing;- if (store.graphs.length >= MAX_GRAPHS) return null;+ if (store.graphs.length >= MAX_GRAPHS) throw new Error("workflow_graph_capacity_reached: 200 graphs; recipe retained, no graphs deleted");const nodes = [{ id: "manual", name: "Manual Trigger", type: "manual" as const, position: { x: 60, y: 120 }, config: {} },...recipe.bestSteps.map((step, index) => ({ id: `step-${index + 1}`, name: step.tool, type: "tool" as const, position: { x: 300 + index * 240, y: 120 }, config: { tool: step.tool, arguments: step.args ?? {}, learnedTarget: step.target ?? "" } })),@@ -174,7 +174,7 @@ export async function listWorkflowGraphTriggerSources(): Promise<Array<{ owner:let owners: string[]; try { owners = await fs.readdir(root()); } catch (error) { if ((error as NodeJS.ErrnoException).code === "ENOENT") return []; throw error; }const out: Array<{ owner: string; principal: string; graph: WorkflowGraph }> = [];for (const owner of owners.filter((value) => /^[a-f0-9]{64}$/.test(value)).slice(0, 4096)) {- const store = await readStore(owner).catch(() => null); if (!store?.principal) continue;+ const store = await readWorkflowGraphStore(owner).catch(() => null); if (!store?.principal) continue;for (const graph of store.graphs) if (graph.status === "active" && graph.nodes.some((node) => node.type === "schedule" || node.type === "webhook" || node.type === "channel_trigger")) out.push({ owner, principal: store.principal, graph });}return out;diff --git a/lib/workflow/learning-graph-receipt.ts b/lib/workflow/learning-graph-receipt.tsnew file mode 100644index 00000000..67fdc54b--- /dev/null+++ b/lib/workflow/learning-graph-receipt.ts@@ -0,0 +1,11 @@+import { ensureLearnedWorkflowGraph } from "./graph-store";+import type { FinishWorkflowResult, LearnedRecipe } from "./types";+export async function learnedGraphReceipt(recipe: LearnedRecipe): Promise<NonNullable<FinishWorkflowResult["graphReceipt"]>> {+ try {+ const graph = await ensureLearnedWorkflowGraph(recipe);+ return graph ? { state: "available", graphId: graph.id } : { state: "skipped" };+ } catch (error) {+ return { state: "warning", warning: error instanceof Error && error.message.startsWith("workflow_graph_capacity_reached")+ ? error.message : "Learned graph persistence failed; recipe retained. Inspect Workflows before retrying." };+ }+}diff --git a/lib/workflow/learning.ts b/lib/workflow/learning.tsindex 07e640af..83567d55 100644--- a/lib/workflow/learning.ts+++ b/lib/workflow/learning.ts@@ -7,7 +7,7 @@ import { safeMemoryText } from "./sanitize";import { actorKey, removeActiveWorkflow, workflowFor } from "./state";import { loadWorkflowStore, persistWorkflowStore } from "./storage";import type { FinishWorkflowResult, LearnedRecipe, RecipeAccess, WorkflowStepProvenance } from "./types";-import { ensureLearnedWorkflowGraph } from "./graph-store";+import { learnedGraphReceipt } from "./learning-graph-receipt";import { archiveLearnedRecipes, listArchivedLearnedRecipes } from "./recipe-archive";import { mergeCandidatePools } from "./candidate-pool";import { rememberAgentMemory } from "@/lib/agent/memory-store";@@ -125,7 +125,7 @@ export async function finishWorkflow(input: {await persistWorkflowStore(store);// Successful sanitized session routes automatically become private graph drafts.// This is best-effort learning: graph persistence must never turn workflow_finish into a failure.- if (input.success && process.env.NODE_ENV !== "test") await ensureLearnedWorkflowGraph(recipe).catch(() => undefined);+ const graphReceipt = input.success ? await learnedGraphReceipt(recipe) : undefined;if (input.success && process.env.NODE_ENV !== "test") {const memoryKey = `workflow:${createHash("sha256").update(`${recipe.normalizedIntent}|${recipe.project ?? ""}`).digest("hex").slice(0, 20)}`;const route = recipe.bestSteps.map((step) => step.tool).join(" → ").slice(0, 1200);@@ -139,7 +139,7 @@ export async function finishWorkflow(input: {const improvedByMs = input.success && previousFastestMs != null && durationMs < previousFastestMs? previousFastestMs - durationMs : undefined;return {- workflow, recipe, currentDurationMs: durationMs,+ workflow, recipe, graphReceipt, currentDurationMs: durationMs,...(previousFastestMs != null ? { previousFastestMs } : {}),...(improvedByMs != null ? { improvedByMs, improvedPct: Math.round((improvedByMs / previousFastestMs!) * 1000) / 10 } : {}),...(recommendedRecipeId ? { reuse: { recommendedRecipeId, routeMatched, score: reuseScore } } : {}),diff --git a/lib/workflow/owner-discovery.ts b/lib/workflow/owner-discovery.tsnew file mode 100644index 00000000..adfdb8da--- /dev/null+++ b/lib/workflow/owner-discovery.ts@@ -0,0 +1,37 @@+// Browser-owner-only metadata discovery. Never used by client/MCP graph dispatch.+import path from "node:path";+import { agentSessionsDir } from "@/lib/agent/session-paths";+import { listWorkflowFiles } from "./private-file";+import { createWorkflowGraph, readWorkflowGraphStore, workflowGraphOwner } from "./graph-store";+import type { WorkflowDiscoveryPage } from "@/lib/contracts/workflow-discovery";+export function requireDiscoveryOwner(role: string) {+ if (role !== "owner") throw new Error("owner_required");+}+export async function discoverOwnerGraphs(role: string, principal: string, offset = 0): Promise<WorkflowDiscoveryPage> {+ requireDiscoveryOwner(role);+ let owners: string[];+ try { owners = (await listWorkflowFiles(path.join(agentSessionsDir(), ".workflow-graphs"))).filter(x => /^[a-f0-9]{64}$/.test(x)).sort(); }+ catch (error) { if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; owners = []; }+ const start = Math.max(0, Math.trunc(offset) || 0), selected = owners.slice(start, start + 4);+ const graphs: WorkflowDiscoveryPage["graphs"] = [], warnings: string[] = [];+ for (const owner of selected) {+ try {+ const store = await readWorkflowGraphStore(owner);+ if (!store.principal || workflowGraphOwner(store.principal) !== owner) { warnings.push(`Unresolved origin: ${owner}`); continue; }+ graphs.push(...store.graphs.map(g => ({ id: g.id, name: g.name, status: g.status, project: g.metadata.project,+ updatedAt: g.updatedAt, originPrincipal: store.principal, owner, readOnly: store.principal !== principal, nodeCount: g.nodes.length })));+ } catch { warnings.push(`Graph metadata unavailable: ${owner}`); }+ }+ return { graphs, scan: { ownerOffset: start, ownersScanned: selected.length, totalOwners: owners.length,+ ...(start + selected.length < owners.length ? { nextOwnerOffset: start + selected.length } : {}), warnings } };+}+export async function cloneOwnerGraph(role: string, principal: string, owner: string, id: string) {+ requireDiscoveryOwner(role);+ const store = await readWorkflowGraphStore(owner);+ if (!store.principal || workflowGraphOwner(store.principal) !== owner) throw new Error("unresolved graph origin");+ const graph = store.graphs.find(g => g.id === id);+ if (!graph) throw new Error("workflow graph not found");+ // Copy into caller namespace; the source and its immutable origin are untouched.+ return createWorkflowGraph(principal, { ...graph, id: undefined, name: `${graph.name} Copy`.slice(0, 160), status: "draft",+ metadata: { ...graph.metadata, provenance: "clone", fingerprint: undefined } });+}diff --git a/lib/workflow/types.ts b/lib/workflow/types.tsindex a35f9ca4..bedc9e6f 100644--- a/lib/workflow/types.ts+++ b/lib/workflow/types.ts@@ -116,6 +116,7 @@ export type WorkflowStoreState = {};export type FinishWorkflowResult = {+ graphReceipt?: { state: "available" | "skipped" | "warning"; graphId?: string; warning?: string };workflow: ActiveWorkflow;recipe: LearnedRecipe;currentDurationMs: number;