Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

Keep startup workspace context out of saved folder choices

justrach54b09406a2b9
diff --git a/apps/native/components/primitives/SidebarNav.tsx b/apps/native/components/primitives/SidebarNav.tsx
index 8046c4b7..de69a187 100644
--- a/apps/native/components/primitives/SidebarNav.tsx
+++ b/apps/native/components/primitives/SidebarNav.tsx
@@ -93,7 +93,7 @@ type SidebarNavProps = {
onWorkspaceSettings?: () => void;
};
-export type SidebarWorkspace = { path: string; name: string };
+export type SidebarWorkspace = { path: string; name: string; source?: "saved" | "startup" };
const SIDEBAR_MOTION = {
expandedWidth: 248,
diff --git a/apps/native/components/primitives/WorkspaceMenu.tsx b/apps/native/components/primitives/WorkspaceMenu.tsx
index b2fcabba..2e0bd9b7 100644
--- a/apps/native/components/primitives/WorkspaceMenu.tsx
+++ b/apps/native/components/primitives/WorkspaceMenu.tsx
@@ -2,7 +2,7 @@
import { useEffect, useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { monogram } from '@/lib/workspaces';
-type Row={path:string;name:string};
+type Row={path:string;name:string;source?:"saved"|"startup"};
export default function WorkspaceMenu({position,current,rows,onSwitch,onNew,onSettings,onClose}:{
position:{top:number;left:number};current?:string;rows:Row[];onSwitch?:(path:string)=>void;onNew?:()=>void;onSettings?:()=>void;onClose:()=>void;
}) {
@@ -25,7 +25,7 @@ export default function WorkspaceMenu({position,current,rows,onSwitch,onNew,onSe
{list.map(row=><button type="button" key={row.path} aria-current={row.path===current?'true':undefined} title={row.path} onClick={pick(()=>onSwitch?.(row.path))}
className="flex w-full items-center gap-2 rounded-lg px-2 py-2 text-left hover:bg-hover focus-visible:bg-hover focus-visible:outline-none">
<span aria-hidden className="flex size-8 shrink-0 items-center justify-center rounded-lg bg-field text-xs text-ink-2">{monogram(row.name)}</span>
- <span className="min-w-0 flex-1"><span className="block truncate text-sm font-medium text-ink">{row.name}</span><span className="block truncate font-mono text-[10px] text-ink-3">{row.path}</span></span>
+ <span className="min-w-0 flex-1"><span className="block truncate text-sm font-medium text-ink">{row.name}</span><span className="block truncate font-mono text-[10px] text-ink-3">{row.source==='startup'?'Startup folder':'Saved folder'} · {row.path}</span></span>
{row.path===current&&<span className="text-xs text-accent">Current</span>}
</button>)}
{!list.length&&<p className="p-3 text-sm text-ink-3">{query?'No matching workspaces.':'Open a folder to start a workspace.'}</p>}
diff --git a/apps/native/components/site/GraffHarness.tsx b/apps/native/components/site/GraffHarness.tsx
index 11649ce9..ea8ab0b3 100644
--- a/apps/native/components/site/GraffHarness.tsx
+++ b/apps/native/components/site/GraffHarness.tsx
@@ -477,7 +477,7 @@ export default function GraffHarness() {
setConversationsOpen(key === "conversations");
}}
workspace={sidebarWorkspace}
- workspaces={workspaces.map((w) => ({ path: w.path, name: w.name }))}
+ workspaces={workspaces}
onSwitchWorkspace={switchWorkspace}
onArchiveRecent={(id) => dropStored(id, true)}
onDeleteRecent={(id) => dropStored(id, false)}
diff --git a/apps/native/components/site/harness-workspace-actions.ts b/apps/native/components/site/harness-workspace-actions.ts
index a4926423..3976641c 100644
--- a/apps/native/components/site/harness-workspace-actions.ts
+++ b/apps/native/components/site/harness-workspace-actions.ts
@@ -67,7 +67,7 @@ export function workspaceActions({workspacesRef, activePathRef, chatsRef, chatId
};
const addWorkspace = (path: string) => {
- const list = upsertWorkspace(workspacesRef.current, { path, name: basename(path) });
+ const list = upsertWorkspace(workspacesRef.current, { path, name: basename(path), source: "saved" });
persistWorkspaces(list);
setDialog(null);
const added = findWorkspace(list, path);
@@ -75,7 +75,7 @@ export function workspaceActions({workspacesRef, activePathRef, chatsRef, chatId
};
const saveWorkspace = (ws: Workspace) => {
- persistWorkspaces(upsertWorkspace(workspacesRef.current, ws));
+ persistWorkspaces(upsertWorkspace(workspacesRef.current, { ...ws, source: "saved" }));
setDialog(null);
};
diff --git a/apps/native/components/site/useHarnessSessions.ts b/apps/native/components/site/useHarnessSessions.ts
index 590e9989..9e5dd8b3 100644
--- a/apps/native/components/site/useHarnessSessions.ts
+++ b/apps/native/components/site/useHarnessSessions.ts
@@ -4,7 +4,7 @@ import type { AcpCommand } from "@/lib/acp";
import type { PromptModel } from "@/components/primitives/PromptBar";
import { listSessionsPage, type StoredSession } from "@/lib/sessions";
import { restoreProjects, persistProjects } from "@/lib/project-preferences";
-import { basename, findWorkspace, upsertWorkspace, type Workspace } from "@/lib/workspaces";
+import { findWorkspace, restoreWorkspaceSelection, type Workspace } from "@/lib/workspaces";
import { newSessionName, type Chat } from "./harness-types";
type Ref<T> = MutableRefObject<T>;
type Setter<T> = Dispatch<SetStateAction<T>>;
@@ -88,15 +88,11 @@ export function useHarnessSessions({sessionsRef, sessionNamesRef, chatsRef, work
if (cancelled) return;
setHealth(h);
if (!h.ok) return;
- // The server's default workspace is always a row; the remembered pick
- // wins when it is still listed, else the default is active.
+ // Keep startup context visible without persisting it as a folder choice.
const root = h.cwd ?? "";
const savedProjects = await restoreProjects(window.localStorage);
if (cancelled) return;
- let list = savedProjects.list;
- if (root && !findWorkspace(list, root)) list = upsertWorkspace(list, { path: root, name: basename(root) });
- const remembered = savedProjects.active;
- const active = findWorkspace(list, remembered)?.path ?? findWorkspace(list, root)?.path ?? list[0]?.path ?? null;
+ const { list, active } = restoreWorkspaceSelection(savedProjects.list, savedProjects.active, root);
workspacesRef.current = list;
activePathRef.current = active;
setWorkspaces(list);
diff --git a/apps/native/electron/frontend-runtime.cjs b/apps/native/electron/frontend-runtime.cjs
index d5160dbd..5938375f 100644
--- a/apps/native/electron/frontend-runtime.cjs
+++ b/apps/native/electron/frontend-runtime.cjs
@@ -165,6 +165,16 @@ app.whenReady().then(async () => {
await until(() => js(`document.querySelector('textarea[aria-label="Prompt"]').value === ${JSON.stringify(text)} && !document.querySelector('[aria-label="Send"]').disabled`), 'typed prompt ready');
await click('[aria-label="Send"]');
};
+ if (!process.env.GRAFF_CLI_TEST) {
+ assert.equal((await projects.load())?.list.some(row => row.path === workspace), false, 'Startup alone must not save a project');
+ await click('[data-workspace-trigger]');
+ await until(() => js(`!!document.querySelector('[data-workspace-menu]') && document.querySelector('[data-workspace-menu]').textContent.includes('Startup folder')`), 'startup folder provenance');
+ await js(`new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)))`);
+ await sleep(150);
+ fs.writeFileSync(path.join(output, 'workspace-startup-source.png'), (await wc.capturePage()).toPNG());
+ await click('[data-workspace-trigger]');
+ report.passed.push('startup workspace stays available with explicit provenance and is absent from durable project preferences');
+ }
if(htmlTool) return require('./html-tool-frontend.cjs').runHtmlTool({win,origin,output,temp,workspace,requests,send,click,until,report});
await send('Write the fixture file, then finish.');
await until(() => js(`document.body.textContent.includes('Scripted final request rejected') && !document.querySelector('article[aria-busy="true"]')`), 'real failure displayed', 25000);
diff --git a/apps/native/lib/project-preferences.ts b/apps/native/lib/project-preferences.ts
index efee84ec..1144e26e 100644
--- a/apps/native/lib/project-preferences.ts
+++ b/apps/native/lib/project-preferences.ts
@@ -1,5 +1,5 @@
import { desktop } from './desktop';
-import { loadWorkspaces, loadActiveWorkspace, saveWorkspaces, saveActiveWorkspace, type Workspace } from './workspaces';
+import { loadWorkspaces, loadActiveWorkspace, saveWorkspaces, saveActiveWorkspace, savedWorkspaceChoices, findWorkspace, type Workspace } from './workspaces';
export async function restoreProjects(storage: Storage) {
try {
const saved = await desktop()?.projects?.('load');
@@ -8,8 +8,10 @@ export async function restoreProjects(storage: Storage) {
return { list: loadWorkspaces(storage), active: loadActiveWorkspace(storage) };
}
export function persistProjects(storage: Storage, list: Workspace[], active: string | null) {
- saveWorkspaces(storage, list); saveActiveWorkspace(storage, active);
- void desktop()?.projects?.('save', { list, active }).catch(() => {
+ const choices = savedWorkspaceChoices(list);
+ const savedActive = findWorkspace(choices, active)?.path ?? null;
+ saveWorkspaces(storage, choices); saveActiveWorkspace(storage, savedActive);
+ void desktop()?.projects?.('save', { list: choices, active: savedActive }).catch(() => {
// The browser copy remains available if the settings volume is unavailable.
});
}
diff --git a/apps/native/lib/workspaces.test.ts b/apps/native/lib/workspaces.test.ts
index 90335668..59c39cc3 100644
--- a/apps/native/lib/workspaces.test.ts
+++ b/apps/native/lib/workspaces.test.ts
@@ -2,6 +2,7 @@ import { describe, it } from "node:test";
import assert from "node:assert/strict";
import {
basename,
+ restoreWorkspaceSelection,
findWorkspace,
loadActiveWorkspace,
loadWorkspaces,
@@ -98,3 +99,42 @@ describe("shellQuote", () => {
assert.equal(shellQuote("/it's"), "'/it'\\''s'");
});
});
+
+
+it("does not turn an unused startup directory into a saved project", () => {
+ const store = memoryStorage();
+ saveWorkspaces(store, [
+ { path: "/chosen", name: "Chosen", model: "test-model" },
+ { path: "/incidental/nested", name: "nested", source: "startup" } as Workspace,
+ ]);
+ assert.deepEqual(loadWorkspaces(store), [{ path: "/chosen", name: "Chosen", model: "test-model" }]);
+});
+
+
+it("startup context does not evict any of fifty deliberate folder choices", () => {
+ const saved = Array.from({ length: 50 }, (_, i) => ({ path: `/chosen/${i}`, name: String(i) }));
+ const result = restoreWorkspaceSelection(saved, "/chosen/0", "/incidental");
+ assert.equal(result.list.length, 51);
+ assert.equal(result.active, "/chosen/0");
+ const store = memoryStorage(); saveWorkspaces(store, result.list);
+ assert.deepEqual(loadWorkspaces(store), saved);
+});
+
+it("an explicitly saved startup folder stays saved with its settings", () => {
+ const saved: Workspace[] = [{ path: "/chosen", name: "Custom name", yolo: false }];
+ const result = restoreWorkspaceSelection(saved, null, "/chosen/");
+ assert.deepEqual(result, { list: saved, active: "/chosen" });
+ const store = memoryStorage(); saveWorkspaces(store, result.list);
+ assert.deepEqual(loadWorkspaces(store), saved);
+});
+
+
+it("startup context does not consume a slot when another chosen folder is added", () => {
+ const saved = Array.from({ length: 50 }, (_, i) => ({ path: `/chosen/${i}`, name: String(i) }));
+ const { list } = restoreWorkspaceSelection(saved, null, "/incidental");
+ const updated = upsertWorkspace(list, { path: "/new-choice", name: "New", source: "saved" });
+ const store = memoryStorage(); saveWorkspaces(store, updated);
+ assert.equal(loadWorkspaces(store).length, 50);
+ assert.equal(loadWorkspaces(store)[0].path, "/chosen/1");
+ assert.equal(updated.length, 51);
+});
diff --git a/apps/native/lib/workspaces.ts b/apps/native/lib/workspaces.ts
index 7c5242c5..6cb84fb6 100644
--- a/apps/native/lib/workspaces.ts
+++ b/apps/native/lib/workspaces.ts
@@ -10,6 +10,8 @@ export const WORKSPACES_MAX = 50;
export type Workspace = {
/** Absolute directory — the row's identity. */
path: string;
+ /** Startup context is visible, but is not a saved folder choice. */
+ source?: "saved" | "startup";
/** Display name; defaults to the folder's basename. */
name: string;
/** Model new tabs in this workspace spawn with; unset = the harness pick. */
@@ -50,7 +52,10 @@ export function upsertWorkspace(list: readonly Workspace[], ws: Workspace, max =
const next: Workspace = { ...ws, path, name };
const idx = list.findIndex((w) => w.path === path);
const out = idx >= 0 ? list.map((w, i) => (i === idx ? { ...w, ...next } : w)) : [...list, next];
- return out.length > max ? out.slice(out.length - max) : out;
+ const choices = out.filter(row => row.source !== "startup");
+ if (choices.length <= max) return out;
+ const keep = new Set(choices.slice(-max).map(row => row.path));
+ return out.filter(row => row.source === "startup" || keep.has(row.path));
}
export function removeWorkspace(list: readonly Workspace[], path: string): Workspace[] {
@@ -64,6 +69,19 @@ export function findWorkspace(list: readonly Workspace[], path: string | null |
return list.find((w) => w.path === p);
}
+/** Preserve chosen rows, including a full list, when showing startup context. */
+export function restoreWorkspaceSelection(saved: readonly Workspace[], remembered: string | null, root: string) {
+ const list = [...saved];
+ const normalized = normalizePath(root);
+ if (normalized && !findWorkspace(list, normalized)) list.push({ path: normalized, name: basename(normalized), source: "startup" });
+ const active = findWorkspace(list, remembered)?.path ?? findWorkspace(list, normalized)?.path ?? list[0]?.path ?? null;
+ return { list, active };
+}
+
+export function savedWorkspaceChoices(list: readonly Workspace[]): Workspace[] {
+ return list.filter(row => row.source !== "startup").slice(-WORKSPACES_MAX);
+}
+
type ReadStore = Pick<Storage, "getItem">;
type WriteStore = Pick<Storage, "setItem" | "removeItem">;
@@ -87,7 +105,7 @@ export function loadWorkspaces(storage: ReadStore | null | undefined): Workspace
export function saveWorkspaces(storage: WriteStore | null | undefined, list: readonly Workspace[]): void {
try {
- storage?.setItem(WORKSPACES_KEY, JSON.stringify(list.slice(-WORKSPACES_MAX)));
+ storage?.setItem(WORKSPACES_KEY, JSON.stringify(savedWorkspaceChoices(list)));
} catch {
// Private mode or a full quota: the list is a convenience, never a failure.
}
diff --git a/docs/adr/0080-desktop-projects-are-folders.md b/docs/adr/0080-desktop-projects-are-folders.md
index dc7cd9d3..93a0fc23 100644
--- a/docs/adr/0080-desktop-projects-are-folders.md
+++ b/docs/adr/0080-desktop-projects-are-folders.md
@@ -20,6 +20,12 @@ validated main-process IPC and serialized atomic writes. Existing browser values
are migrated on first use and retained as a fallback. Preferences contain folder
choices, not conversation history. The web client continues using browser storage.
+Startup context is shown separately from saved folder choices. Merely launching
+in a directory does not save it as a project or evict a deliberate choice from
+the saved-list limit. Opening a folder or saving its settings records a choice.
+Existing preferences without source metadata remain saved entries; their
+presence alone is not evidence of earlier session activity.
+
## Validation
The project-store test covers restart persistence and ordered writes. The offline