Skip to content

The open-source agent harness - the runtime layer that turns an LLM into a working agent.

Preserve active agent draft when returning to Build Agent from other pages. Updated ShellModeContext to maintain draft state (#692)

harshil-209698ab384b8b2d
diff --git a/.changeset/preserve-agent-builder-draft.md b/.changeset/preserve-agent-builder-draft.md
new file mode 100644
index 000000000..96c064caa
--- /dev/null
+++ b/.changeset/preserve-agent-builder-draft.md
@@ -0,0 +1,5 @@
+---
+'@truefoundry/trueforge-ui': patch
+---
+
+Preserve the active agent draft when returning to Build Agent from another page.
diff --git a/packages/trueforge-ui/src/server/ShellModeContext.tsx b/packages/trueforge-ui/src/server/ShellModeContext.tsx
index 3cb8d7a64..9d8901953 100644
--- a/packages/trueforge-ui/src/server/ShellModeContext.tsx
+++ b/packages/trueforge-ui/src/server/ShellModeContext.tsx
@@ -483,11 +483,31 @@ export function ShellModeProvider({
selectLibraryAgent({ isMutable: true, isCreateAgent: false, agentSpec: chatSeedRef.current });
}, [isComposerEnabled, refreshCapabilities, selectLibraryAgent]);
+ const isActiveAgentBuilder =
+ effectiveMode.status === 'active' && effectiveMode.isMutable && effectiveMode.isCreateAgent;
const openAgentBuilder = useCallback(() => {
if (!isComposerEnabled) return;
refreshCapabilities?.();
+ // Returning from an overlay must keep the live draft runtime and its unsaved instructions.
+ if (isActiveAgentBuilder) {
+ setSettingsOpen(false);
+ setLibraryOpenState(false);
+ setLibraryAgentId(null);
+ setSessionsOpen(false);
+ setSchedulesOpen(false);
+ setAgentConfigOpenState(true);
+ return;
+ }
selectLibraryAgent({ isMutable: true, isCreateAgent: true, agentSpec: agentSeedRef.current });
- }, [isComposerEnabled, refreshCapabilities, selectLibraryAgent]);
+ }, [
+ isActiveAgentBuilder,
+ isComposerEnabled,
+ refreshCapabilities,
+ selectLibraryAgent,
+ setSchedulesOpen,
+ setSessionsOpen,
+ setSettingsOpen,
+ ]);
const sandboxEnabled = capabilities?.sandbox.enabled;
const rememberDraftSpec = useCallback(
@@ -497,7 +517,8 @@ export function ShellModeProvider({
if (kind === 'chat') {
chatSeedRef.current = preferences;
} else {
- agentSeedRef.current = preferences;
+ // Keep the active builder intact in memory; storage remains limited to reusable preferences.
+ agentSeedRef.current = withCapabilitiesSandbox(agentSpec, sandboxEnabled);
}
writeDraftSpecPreferences(kind, preferences);
},
diff --git a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx b/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
index 93832813c..7ae238d06 100644
--- a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
+++ b/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
@@ -177,6 +177,43 @@ describe('ShellModeProvider', () => {
expect(result.current.agentConfigOpen).toBe(false);
});
+ it('resumes the active agent builder without resetting its runtime', () => {
+ const { result } = renderHook(() => useShellMode(), { wrapper: wrap() });
+
+ act(() => result.current.openAgentBuilder());
+ const builderRuntimeKey = result.current.runtimeKey;
+
+ act(() => result.current.setLibraryOpen(true));
+ expect(result.current.libraryOpen).toBe(true);
+ expect(result.current.agentConfigOpen).toBe(false);
+
+ act(() => result.current.openAgentBuilder());
+ expect(result.current.libraryOpen).toBe(false);
+ expect(result.current.agentConfigOpen).toBe(true);
+ expect(result.current.runtimeKey).toBe(builderRuntimeKey);
+ });
+
+ it('restores the active agent draft after visiting New Chat', () => {
+ const { result } = renderHook(() => useShellMode(), { wrapper: wrap() });
+ const agentDraft = {
+ model: { name: 'chosen/model' },
+ instructions: 'Keep these instructions.',
+ };
+
+ act(() => result.current.openAgentBuilder());
+ act(() => result.current.rememberDraftSpec(agentDraft, 'agent'));
+ act(() => result.current.openDraft());
+ act(() => result.current.openAgentBuilder());
+
+ expect(result.current.mode).toMatchObject({
+ status: 'active',
+ isMutable: true,
+ isCreateAgent: true,
+ agentSpec: agentDraft,
+ });
+ expect(readDraftSpecPreferences('agent')).not.toHaveProperty('instructions');
+ });
+
it('openDraft starts New Chat without agent config; openAgentBuilder opens config', () => {
const { result } = renderHook(() => useShellMode(), { wrapper: wrap() });