Skip to content

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

fix: added withCapabilitiesSandbox function to manage sandbox configuration in agent specs (#296)

Vinit53104b11f317
diff --git a/.changeset/two-ads-tell.md b/.changeset/two-ads-tell.md
new file mode 100644
index 000000000..f82a5e380
--- /dev/null
+++ b/.changeset/two-ads-tell.md
@@ -0,0 +1,5 @@
+---
+'@truefoundry/trueforge-ui': patch
+---
+
+Keep active and remembered draft sandbox settings synchronized with server capabilities.
diff --git a/packages/trueforge-ui/src/atoms/draft/DraftSpecPreferenceBridge.tsx b/packages/trueforge-ui/src/atoms/draft/DraftSpecPreferenceBridge.tsx
index 8da238b89..74dd0eac8 100644
--- a/packages/trueforge-ui/src/atoms/draft/DraftSpecPreferenceBridge.tsx
+++ b/packages/trueforge-ui/src/atoms/draft/DraftSpecPreferenceBridge.tsx
@@ -3,6 +3,7 @@
import { useTrueFoundryAgentSpec, useTrueFoundryUpdateAgentSpec } from '@truefoundry/assistant-ui-runtime';
import { useEffect } from 'react';
+import { withCapabilitiesSandbox } from '../../server/draftSpecPreferences.js';
import { useServerCapabilities } from '../../server/ServerContext.js';
import { useShellMode } from '../../server/ShellModeContext.js';
import type { AgentSkill, AgentSpec, ConnectorState, ModelSelection } from '../../server/types.js';
@@ -70,6 +71,17 @@ export function reconcileDraftSpecPreferences({
return update;
}
+export function reconcileDraftSandbox({
+ agentSpec,
+ sandboxEnabled,
+}: {
+ agentSpec: AgentSpec;
+ sandboxEnabled: boolean | null | undefined;
+}): Partial<AgentSpec> {
+ const nextSpec = withCapabilitiesSandbox(agentSpec, sandboxEnabled);
+ return nextSpec === agentSpec ? {} : { config: nextSpec.config };
+}
+
/**
* Mirrors plain-draft composer choices into the shell seed and removes catalog
* entries that disappeared since those choices were stored.
@@ -79,6 +91,7 @@ export function DraftSpecPreferenceBridge() {
const { agentSpec } = useTrueFoundryAgentSpec();
const updateAgentSpec = useTrueFoundryUpdateAgentSpec();
const capabilities = useServerCapabilities();
+ const sandboxEnabled = capabilities?.sandbox.enabled;
const { models, skills, connectors, loaded, error, ensureLoaded } = useDraftCatalog();
const isPlainDraft = mode.status === 'active' && mode.isMutable && mode.agentId == null && pendingSessionId == null;
@@ -92,6 +105,14 @@ export function DraftSpecPreferenceBridge() {
}
}, [agentSpec, isPlainDraft, rememberDraftSpec]);
+ useEffect(() => {
+ if (!isPlainDraft || agentSpec == null || updateAgentSpec == null) return;
+ const update = reconcileDraftSandbox({ agentSpec, sandboxEnabled });
+ if (Object.keys(update).length > 0) {
+ updateAgentSpec(update);
+ }
+ }, [agentSpec, isPlainDraft, sandboxEnabled, updateAgentSpec]);
+
useEffect(() => {
if (!isPlainDraft || agentSpec == null || updateAgentSpec == null || !loaded || error != null) return;
diff --git a/packages/trueforge-ui/src/server/ShellModeContext.tsx b/packages/trueforge-ui/src/server/ShellModeContext.tsx
index 1d9ff0a35..6012a0301 100644
--- a/packages/trueforge-ui/src/server/ShellModeContext.tsx
+++ b/packages/trueforge-ui/src/server/ShellModeContext.tsx
@@ -5,6 +5,7 @@ import { createContext, useCallback, useContext, useEffect, useMemo, useRef, use
import {
readDraftSpecPreferences,
selectDraftSpecPreferences,
+ withCapabilitiesSandbox,
writeDraftSpecPreferences,
} from './draftSpecPreferences.js';
import { useOptionalRefreshServerCapabilities, useServerCapabilities } from './ServerContext.js';
@@ -295,12 +296,16 @@ export function ShellModeProvider({
selectLibraryAgent({ isMutable: true, agentSpec: mutableSeedRef.current });
}, [isComposerEnabled, refreshCapabilities, selectLibraryAgent]);
- const rememberDraftSpec = useCallback((agentSpec: AgentSpec) => {
- const preferences = selectDraftSpecPreferences(agentSpec);
- rememberedSpecRef.current = preferences;
- mutableSeedRef.current = preferences;
- writeDraftSpecPreferences(preferences);
- }, []);
+ const sandboxEnabled = capabilities?.sandbox.enabled;
+ const rememberDraftSpec = useCallback(
+ (agentSpec: AgentSpec) => {
+ const preferences = withCapabilitiesSandbox(selectDraftSpecPreferences(agentSpec), sandboxEnabled);
+ rememberedSpecRef.current = preferences;
+ mutableSeedRef.current = preferences;
+ writeDraftSpecPreferences(preferences);
+ },
+ [sandboxEnabled],
+ );
const openHistorySession = useCallback(
({
diff --git a/packages/trueforge-ui/src/server/draftSpecPreferences.ts b/packages/trueforge-ui/src/server/draftSpecPreferences.ts
index 5c4849e60..e455e333c 100644
--- a/packages/trueforge-ui/src/server/draftSpecPreferences.ts
+++ b/packages/trueforge-ui/src/server/draftSpecPreferences.ts
@@ -47,6 +47,29 @@ export function selectDraftSpecPreferences(spec: AgentSpec): AgentSpec {
};
}
+function readSandboxEnabled(spec: AgentSpec): boolean | undefined {
+ if (spec.config === undefined) return undefined;
+ // `sandbox` is persisted on draft config but is not part of AgentRuntimeConfig.
+ const sandbox = Reflect.get(spec.config, 'sandbox');
+ if (!isObject(sandbox)) return undefined;
+ const enabled = Reflect.get(sandbox, 'enabled');
+ return typeof enabled === 'boolean' ? enabled : undefined;
+}
+
+/** Align draft `config.sandbox.enabled` with loaded server capabilities; no-op while unknown. */
+export function withCapabilitiesSandbox(spec: AgentSpec, sandboxEnabled: boolean | null | undefined): AgentSpec {
+ if (sandboxEnabled == null) return spec;
+ if (readSandboxEnabled(spec) === sandboxEnabled) return spec;
+ const config = {
+ ...spec.config,
+ sandbox: { enabled: sandboxEnabled },
+ };
+ return {
+ ...spec,
+ config,
+ };
+}
+
export function readDraftSpecPreferences(): AgentSpec | null {
if (typeof window === 'undefined') return null;
try {
diff --git a/packages/trueforge-ui/test/atoms/draft/DraftCatalogProvider.test.tsx b/packages/trueforge-ui/test/atoms/draft/DraftCatalogProvider.test.tsx
index c1ced2870..02e26fff0 100644
--- a/packages/trueforge-ui/test/atoms/draft/DraftCatalogProvider.test.tsx
+++ b/packages/trueforge-ui/test/atoms/draft/DraftCatalogProvider.test.tsx
@@ -3,7 +3,8 @@ import { act, fireEvent, render, screen, waitFor } from '@testing-library/react'
import { describe, expect, it, vi } from 'vitest';
import { DraftCatalogProvider, useDraftCatalog } from '@/atoms/draft/DraftCatalogProvider.js';
-import { reconcileDraftSpecPreferences } from '@/atoms/draft/DraftSpecPreferenceBridge.js';
+import { reconcileDraftSandbox, reconcileDraftSpecPreferences } from '@/atoms/draft/DraftSpecPreferenceBridge.js';
+import { withCapabilitiesSandbox } from '@/server/draftSpecPreferences.js';
import { ServerProvider } from '@/server/ServerContext.js';
import type { AgentSkill, ConnectorState, ModelSelection } from '@/server/types.js';
import { createMockAgentUIServer } from '../../server/mockServer.js';
@@ -57,6 +58,33 @@ function deferred<T>() {
}
describe('DraftCatalogProvider', () => {
+ it('reconciles loaded sandbox capabilities into the active draft config', () => {
+ const update = reconcileDraftSandbox({
+ agentSpec: { model: { name: 'model' } },
+ sandboxEnabled: true,
+ });
+
+ expect(update).toEqual({
+ config: { sandbox: { enabled: true } },
+ });
+ });
+
+ it('does not update an active draft whose sandbox already matches capabilities', () => {
+ const update = reconcileDraftSandbox({
+ agentSpec: withCapabilitiesSandbox({ model: { name: 'model' } }, true),
+ sandboxEnabled: true,
+ });
+
+ expect(update).toEqual({});
+ });
+
+ it('does not update an active draft while sandbox capabilities are unavailable', () => {
+ const agentSpec = withCapabilitiesSandbox({ model: { name: 'model' } }, true);
+
+ expect(reconcileDraftSandbox({ agentSpec, sandboxEnabled: undefined })).toEqual({});
+ expect(reconcileDraftSandbox({ agentSpec, sandboxEnabled: null })).toEqual({});
+ });
+
it('prunes unavailable remembered choices and falls back to the live model catalog', () => {
const update = reconcileDraftSpecPreferences({
agentSpec: {
diff --git a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx b/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
index cf676eac7..de1de30be 100644
--- a/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
+++ b/packages/trueforge-ui/test/server/ShellModeContext.test.tsx
@@ -1,10 +1,17 @@
// @vitest-environment jsdom
-import { act, renderHook } from '@testing-library/react';
+import { act, renderHook, waitFor } from '@testing-library/react';
import type { ReactNode } from 'react';
-import { beforeEach, describe, expect, it } from 'vitest';
-
-import { DRAFT_SPEC_PREFERENCES_STORAGE_KEY } from '@/server/draftSpecPreferences.js';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import {
+ DRAFT_SPEC_PREFERENCES_STORAGE_KEY,
+ readDraftSpecPreferences,
+ withCapabilitiesSandbox,
+} from '@/server/draftSpecPreferences.js';
+import { ServerProvider, useServerCapabilities } from '@/server/ServerContext.js';
import { ShellModeProvider, useOptionalShellMode, useShellMode, type AgentConfig } from '@/server/ShellModeContext.js';
+import type { AgentUIServer } from '@/server/types.js';
+import { createMockAgentUIServer } from './mockServer.js';
function wrap(agentConfig?: AgentConfig, initialSettingsOpen?: boolean) {
return function Wrapper({ children }: { children: ReactNode }) {
@@ -16,6 +23,16 @@ function wrap(agentConfig?: AgentConfig, initialSettingsOpen?: boolean) {
};
}
+function wrapWithServer(server: AgentUIServer, agentConfig?: AgentConfig) {
+ return function Wrapper({ children }: { children: ReactNode }) {
+ return (
+ <ServerProvider server={server}>
+ <ShellModeProvider agentConfig={agentConfig}>{children}</ShellModeProvider>
+ </ServerProvider>
+ );
+ };
+}
+
describe('ShellModeProvider', () => {
beforeEach(() => {
window.localStorage.removeItem(DRAFT_SPEC_PREFERENCES_STORAGE_KEY);
@@ -437,6 +454,71 @@ describe('ShellModeProvider', () => {
expect(result.current.mode.agentSpec).not.toHaveProperty('instructions');
});
+ it('preserves a host-seeded sandbox while capabilities are unavailable', () => {
+ const hostSeed = withCapabilitiesSandbox({ model: { name: 'chosen/model' } }, true);
+ const { result } = renderHook(() => useShellMode(), {
+ wrapper: wrap({ mode: 'AgentComposer', defaultAgentSpec: hostSeed }),
+ });
+
+ act(() => result.current.rememberDraftSpec(hostSeed));
+
+ expect(readDraftSpecPreferences()).toEqual({
+ model: { name: 'chosen/model' },
+ config: { sandbox: { enabled: true } },
+ });
+ });
+
+ it('stores sandbox from loaded server capabilities', async () => {
+ const getCapabilities = vi.fn(async () => ({
+ data: {
+ sandbox: { enabled: true },
+ skill: { enabled: true },
+ },
+ }));
+ const server = createMockAgentUIServer({ getCapabilities });
+ const { result } = renderHook(
+ () => ({
+ shell: useShellMode(),
+ capabilities: useServerCapabilities(),
+ }),
+ { wrapper: wrapWithServer(server, { mode: 'AgentComposer' }) },
+ );
+ await waitFor(() => expect(result.current.capabilities?.sandbox.enabled).toBe(true));
+
+ act(() => result.current.shell.rememberDraftSpec({ model: { name: 'chosen/model' } }));
+
+ expect(readDraftSpecPreferences()).toEqual({
+ model: { name: 'chosen/model' },
+ config: { sandbox: { enabled: true } },
+ });
+ });
+
+ it('overrides a host-seeded sandbox when loaded capabilities disable it', async () => {
+ const getCapabilities = vi.fn(async () => ({
+ data: {
+ sandbox: { enabled: false },
+ skill: { enabled: false },
+ },
+ }));
+ const server = createMockAgentUIServer({ getCapabilities });
+ const { result } = renderHook(
+ () => ({
+ shell: useShellMode(),
+ capabilities: useServerCapabilities(),
+ }),
+ { wrapper: wrapWithServer(server, { mode: 'AgentComposer' }) },
+ );
+ await waitFor(() => expect(result.current.capabilities?.sandbox.enabled).toBe(false));
+ const hostSeed = withCapabilitiesSandbox({ model: { name: 'chosen/model' } }, true);
+
+ act(() => result.current.shell.rememberDraftSpec(hostSeed));
+
+ expect(readDraftSpecPreferences()).toEqual({
+ model: { name: 'chosen/model' },
+ config: { sandbox: { enabled: false } },
+ });
+ });
+
it('hydrates remembered preferences on first paint', () => {
window.localStorage.setItem(
DRAFT_SPEC_PREFERENCES_STORAGE_KEY,
diff --git a/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts b/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts
new file mode 100644
index 000000000..92d202209
--- /dev/null
+++ b/packages/trueforge-ui/test/server/draftSpecPreferences.test.ts
@@ -0,0 +1,51 @@
+// @vitest-environment jsdom
+import { describe, expect, it } from 'vitest';
+
+import { withCapabilitiesSandbox } from '@/server/draftSpecPreferences.js';
+
+describe('withCapabilitiesSandbox', () => {
+ it('adds sandbox config when config is missing', () => {
+ expect(withCapabilitiesSandbox({ model: { name: 'model' } }, true)).toEqual({
+ model: { name: 'model' },
+ config: { sandbox: { enabled: true } },
+ });
+ });
+
+ it('preserves other runtime config', () => {
+ expect(
+ withCapabilitiesSandbox(
+ {
+ model: { name: 'model' },
+ config: { askUserQuestions: { enabled: false } },
+ },
+ true,
+ ),
+ ).toEqual({
+ model: { name: 'model' },
+ config: {
+ askUserQuestions: { enabled: false },
+ sandbox: { enabled: true },
+ },
+ });
+ });
+
+ it('overrides a spec-owned sandbox flag from capabilities', () => {
+ expect(withCapabilitiesSandbox(withCapabilitiesSandbox({ model: { name: 'model' } }, true), false)).toEqual({
+ model: { name: 'model' },
+ config: { sandbox: { enabled: false } },
+ });
+ });
+
+ it('preserves the spec sandbox while capabilities are unavailable', () => {
+ const spec = withCapabilitiesSandbox({ model: { name: 'model' } }, true);
+
+ expect(withCapabilitiesSandbox(spec, undefined)).toBe(spec);
+ expect(withCapabilitiesSandbox(spec, null)).toBe(spec);
+ });
+
+ it('returns the same spec when sandbox already matches capabilities', () => {
+ const spec = withCapabilitiesSandbox({ model: { name: 'model' } }, true);
+
+ expect(withCapabilitiesSandbox(spec, true)).toBe(spec);
+ });
+});