Skip to content

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

feat: enhance sandbox environment management with secret handling (#932)

Sujai Kumar Guptafde4d4a0661f
diff --git a/.changeset/sandbox-environment-secrets.md b/.changeset/sandbox-environment-secrets.md
new file mode 100644
index 000000000..48cd9afe6
--- /dev/null
+++ b/.changeset/sandbox-environment-secrets.md
@@ -0,0 +1,6 @@
+---
+'@truefoundry/trueforge': patch
+'@truefoundry/trueforge-core': patch
+---
+
+Sync sandbox-environment networking secrets to Daytona org secrets on PUT (plaintext is never stored), keep Daytona refs in `sandbox_environment_secret`, and mount those secret names at sandbox create time.
diff --git a/.github/fern/openapi/openapi.json b/.github/fern/openapi/openapi.json
index a29609952..cb5284a9b 100644
--- a/.github/fern/openapi/openapi.json
+++ b/.github/fern/openapi/openapi.json
@@ -8559,7 +8559,17 @@
}
}
},
- "description": "No sandbox provider configured."
+ "description": "Sandbox provider is missing or its credentials are invalid."
+ },
+ "502": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider secret synchronization failed."
}
},
"summary": "Create or update a sandbox environment",
@@ -8633,6 +8643,26 @@
}
},
"description": "Environment is referenced by one or more agents."
+ },
+ "422": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider is missing or its credentials are invalid."
+ },
+ "502": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider secret deletion failed."
}
},
"summary": "Delete a sandbox environment",
diff --git a/docs/openapi.json b/docs/openapi.json
index a29609952..cb5284a9b 100644
--- a/docs/openapi.json
+++ b/docs/openapi.json
@@ -8559,7 +8559,17 @@
}
}
},
- "description": "No sandbox provider configured."
+ "description": "Sandbox provider is missing or its credentials are invalid."
+ },
+ "502": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider secret synchronization failed."
}
},
"summary": "Create or update a sandbox environment",
@@ -8633,6 +8643,26 @@
}
},
"description": "Environment is referenced by one or more agents."
+ },
+ "422": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider is missing or its credentials are invalid."
+ },
+ "502": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/RequestErrorResponse"
+ }
+ }
+ },
+ "description": "Sandbox provider secret deletion failed."
}
},
"summary": "Delete a sandbox environment",
diff --git a/packages/trueforge-core/src/core/index.ts b/packages/trueforge-core/src/core/index.ts
index 9f9cb7c8b..cf99164bd 100644
--- a/packages/trueforge-core/src/core/index.ts
+++ b/packages/trueforge-core/src/core/index.ts
@@ -212,9 +212,12 @@ export type {
SandboxBuild,
SandboxBuildMetadata,
SandboxBuildStatus,
+ SandboxCreateSecretParams,
+ SandboxDeleteSecretParams,
SandboxExecParams,
SandboxInit,
SandboxProvider,
+ SandboxUpdateSecretParams,
} from './sandbox/provider/Provider';
export { TFYSandboxProvider } from './sandbox/provider/TFYSandboxProvider';
export { SKILL_DOWNLOAD_TIMEOUT_SECONDS, Sandbox, buildWriteAndRunScriptCommand } from './sandbox/Sandbox';
diff --git a/packages/trueforge-core/src/core/sandbox/provider/DaytonaProvider.ts b/packages/trueforge-core/src/core/sandbox/provider/DaytonaProvider.ts
index 1a5818a69..ab1bb2b06 100644
--- a/packages/trueforge-core/src/core/sandbox/provider/DaytonaProvider.ts
+++ b/packages/trueforge-core/src/core/sandbox/provider/DaytonaProvider.ts
@@ -18,7 +18,16 @@ import type { CodeModeTransport } from '../codeMode/CodeModeTransport';
import { CodeModeNatsTransport } from '../codeMode/nats/CodeModeNatsTransport';
import { DEFAULT_PREVIEW_URL_EXPIRY_SECONDS, DEFAULT_SANDBOX_NATS_WS_PORT } from '../constants';
import { DAYTONA_SNAPSHOT_NOT_STARTED_REASON, type DaytonaSandboxEnvironment } from './DaytonaSandboxEnvironment';
-import type { ExecResult, SandboxBuild, SandboxExecParams, SandboxFileInfo, SandboxProvider } from './Provider';
+import type {
+ ExecResult,
+ SandboxBuild,
+ SandboxCreateSecretParams,
+ SandboxDeleteSecretParams,
+ SandboxExecParams,
+ SandboxFileInfo,
+ SandboxProvider,
+ SandboxUpdateSecretParams,
+} from './Provider';
const SANDBOX_NOT_FOUND_STATUS = 404;
const SANDBOX_STATE_STARTED = 'started';
@@ -121,15 +130,41 @@ export class DaytonaSandboxProvider implements SandboxProvider<DaytonaSandboxEnv
envVars?: Record<string, string>;
networkBlockAll?: boolean;
domainAllowList?: string;
+ secrets?: Record<string, string>;
} {
const networking = environment.networking;
return {
...(environment.environment_variables ? { envVars: environment.environment_variables } : {}),
...(networking?.network_block_all ? { networkBlockAll: networking.network_block_all } : {}),
...(networking?.domain_allow_list ? { domainAllowList: networking.domain_allow_list } : {}),
+ ...(environment.mounted_secrets ? { secrets: environment.mounted_secrets } : {}),
};
}
+ /** Create a Daytona secret (value write-only). */
+ async createSecret(params: SandboxCreateSecretParams): Promise<{ id: string; name: string }> {
+ const secret = await this.daytona.secret.create({
+ name: params.name,
+ value: params.value,
+ description: params.description,
+ hosts: params.hosts,
+ });
+ return { id: secret.id, name: secret.name };
+ }
+
+ /** Update a Daytona secret (hosts and optional value). */
+ async updateSecret(params: SandboxUpdateSecretParams): Promise<void> {
+ await this.daytona.secret.update(params.secretId, {
+ hosts: params.hosts,
+ ...(params.value ? { value: params.value } : {}),
+ });
+ }
+
+ /** Delete a Daytona secret by id. */
+ async deleteSecret(params: SandboxDeleteSecretParams): Promise<void> {
+ await this.daytona.secret.delete(params.secretId);
+ }
+
private toBuild({
environment,
state,
diff --git a/packages/trueforge-core/src/core/sandbox/provider/DaytonaSandboxEnvironment.ts b/packages/trueforge-core/src/core/sandbox/provider/DaytonaSandboxEnvironment.ts
index e8d6ff080..8fdc1ab81 100644
--- a/packages/trueforge-core/src/core/sandbox/provider/DaytonaSandboxEnvironment.ts
+++ b/packages/trueforge-core/src/core/sandbox/provider/DaytonaSandboxEnvironment.ts
@@ -22,6 +22,8 @@ export interface DaytonaSandboxEnvironmentOptions {
}
| undefined;
environment_variables?: Record<string, string> | undefined;
+ /** Env var → Daytona org secret name for create. */
+ mounted_secrets?: Record<string, string> | undefined;
/**
* Daytona networking modes are mutually exclusive
* (`network_block_all` vs `domain_allow_list`).
@@ -48,6 +50,7 @@ export interface DaytonaSandboxEnvironment {
resources: DaytonaSandboxEnvironmentOptions['resources'];
image?: DaytonaSandboxEnvironmentOptions['image'];
environment_variables?: DaytonaSandboxEnvironmentOptions['environment_variables'];
+ mounted_secrets?: DaytonaSandboxEnvironmentOptions['mounted_secrets'];
networking?: DaytonaSandboxEnvironmentOptions['networking'];
}
@@ -59,6 +62,7 @@ export function createDaytonaSandboxEnvironment(options: DaytonaSandboxEnvironme
resources: options.resources,
...(options.image !== undefined ? { image: options.image } : {}),
...(options.environment_variables !== undefined ? { environment_variables: options.environment_variables } : {}),
+ ...(options.mounted_secrets !== undefined ? { mounted_secrets: options.mounted_secrets } : {}),
...(options.networking !== undefined ? { networking: options.networking } : {}),
};
}
diff --git a/packages/trueforge-core/src/core/sandbox/provider/Provider.ts b/packages/trueforge-core/src/core/sandbox/provider/Provider.ts
index c0c54119c..5943e0535 100644
--- a/packages/trueforge-core/src/core/sandbox/provider/Provider.ts
+++ b/packages/trueforge-core/src/core/sandbox/provider/Provider.ts
@@ -80,6 +80,26 @@ export interface SandboxBuild {
metadata: SandboxBuildMetadata | null;
}
+/** Create a provider-managed secret for env networking mounts. */
+export interface SandboxCreateSecretParams {
+ name: string;
+ value: string;
+ description: string;
+ hosts: string[];
+}
+
+/** Update a provider-managed secret (hosts and optional value). */
+export interface SandboxUpdateSecretParams {
+ secretId: string;
+ hosts: string[];
+ value?: string | undefined;
+}
+
+/** Delete a provider-managed secret by id. */
+export interface SandboxDeleteSecretParams {
+ secretId: string;
+}
+
/**
* Sandbox backend. `TEnvironment` is provider-specific create/build input
* (`DaytonaSandboxEnvironment` for Daytona; `undefined` when the provider has none).
@@ -101,6 +121,10 @@ export interface SandboxProvider<TEnvironment = undefined> {
/** Env snapshot/image build. Only providers that have environments implement these. */
build?(environment: TEnvironment): Promise<SandboxBuild>;
getBuildStatus?(environment: TEnvironment): Promise<SandboxBuild>;
+ /** Provider-managed secrets for env networking mounts. Unsupported providers throw. */
+ createSecret(params: SandboxCreateSecretParams): Promise<{ id: string; name: string }>;
+ updateSecret(params: SandboxUpdateSecretParams): Promise<void>;
+ deleteSecret(params: SandboxDeleteSecretParams): Promise<void>;
exec(params: SandboxExecParams): Promise<ExecResult>;
/** Provider-specific instructions appended to the agent system prompt. */
getAdditionalInstructions(): string | undefined;
diff --git a/packages/trueforge-core/src/core/sandbox/provider/TFYSandboxProvider.ts b/packages/trueforge-core/src/core/sandbox/provider/TFYSandboxProvider.ts
index 8fe228e1e..bddb77bdf 100644
--- a/packages/trueforge-core/src/core/sandbox/provider/TFYSandboxProvider.ts
+++ b/packages/trueforge-core/src/core/sandbox/provider/TFYSandboxProvider.ts
@@ -92,6 +92,18 @@ export class TFYSandboxProvider implements SandboxProvider {
return Promise.resolve({ sandboxId });
}
+ createSecret(): Promise<{ id: string; name: string }> {
+ return Promise.reject(new Error('TrueFoundry sandbox provider does not support org secrets'));
+ }
+
+ updateSecret(): Promise<void> {
+ return Promise.reject(new Error('TrueFoundry sandbox provider does not support org secrets'));
+ }
+
+ deleteSecret(): Promise<void> {
+ return Promise.reject(new Error('TrueFoundry sandbox provider does not support org secrets'));
+ }
+
/**
* No extra FS jail: exec cwd is the logical sandbox root. Layout getters are cwd-relative
* so writes cannot escape it. `GIT_CONFIG` / PATH / PYTHONPATH must still be absolute so
diff --git a/packages/trueforge-core/tests/core/harnessMocks.ts b/packages/trueforge-core/tests/core/harnessMocks.ts
index 81f6a869c..ddc1cd805 100644
--- a/packages/trueforge-core/tests/core/harnessMocks.ts
+++ b/packages/trueforge-core/tests/core/harnessMocks.ts
@@ -58,10 +58,14 @@ export function makeMockIMCPServer(params: {
}
export function makeStubPublicSandbox(): Sandbox {
+ const unsupportedOrgSecret = () => Promise.reject(new Error('test provider does not support org secrets'));
const provider: SandboxProvider = {
type: 'test',
envSupported: false,
createSandbox: jest.fn(),
+ createSecret: unsupportedOrgSecret,
+ updateSecret: unsupportedOrgSecret,
+ deleteSecret: unsupportedOrgSecret,
exec: jest.fn(),
getAdditionalInstructions: () => undefined,
getToolResultDumpDir: () => '/tmp/tool-results',
diff --git a/packages/trueforge-core/tests/core/sandbox/Sandbox.ids.test.ts b/packages/trueforge-core/tests/core/sandbox/Sandbox.ids.test.ts
index 417ff6e95..8d79b55e4 100644
--- a/packages/trueforge-core/tests/core/sandbox/Sandbox.ids.test.ts
+++ b/packages/trueforge-core/tests/core/sandbox/Sandbox.ids.test.ts
@@ -12,9 +12,13 @@ function readyExec(): Promise<ExecResult> {
function makeProvider(
overrides: Partial<SandboxProvider> & Pick<SandboxProvider, 'createSandbox' | 'exec'>,
): SandboxProvider {
+ const unsupportedOrgSecret = () => Promise.reject(new Error('test provider does not support org secrets'));
return {
type: 'local',
envSupported: false,
+ createSecret: unsupportedOrgSecret,
+ updateSecret: unsupportedOrgSecret,
+ deleteSecret: unsupportedOrgSecret,
getAdditionalInstructions: () => undefined,
getToolResultDumpDir: sandboxId => `${sandboxId}/tool-results`,
getGitCredentialsPath: sandboxId => `${sandboxId}/.git-credentials`,
diff --git a/packages/trueforge-core/tests/core/sandbox/Sandbox.paths.test.ts b/packages/trueforge-core/tests/core/sandbox/Sandbox.paths.test.ts
index 874b34df9..49b004246 100644
--- a/packages/trueforge-core/tests/core/sandbox/Sandbox.paths.test.ts
+++ b/packages/trueforge-core/tests/core/sandbox/Sandbox.paths.test.ts
@@ -11,10 +11,14 @@ function readyExec(): Promise<ExecResult> {
}
function makeProvider(overrides: Partial<SandboxProvider> = {}): SandboxProvider {
+ const unsupportedOrgSecret = () => Promise.reject(new Error('test provider does not support org secrets'));
return {
type: 'test',
envSupported: false,
createSandbox: () => Promise.resolve({ sandboxId: 'raw-1' }),
+ createSecret: unsupportedOrgSecret,
+ updateSecret: unsupportedOrgSecret,
+ deleteSecret: unsupportedOrgSecret,
exec: () => readyExec(),
getAdditionalInstructions: () => undefined,
getToolResultDumpDir: () => '/prov/tool-results',
diff --git a/packages/trueforge-core/tests/core/sandbox/sandboxBridgeTimeout.test.ts b/packages/trueforge-core/tests/core/sandbox/sandboxBridgeTimeout.test.ts
index aee71db01..5e16ed0b5 100644
--- a/packages/trueforge-core/tests/core/sandbox/sandboxBridgeTimeout.test.ts
+++ b/packages/trueforge-core/tests/core/sandbox/sandboxBridgeTimeout.test.ts
@@ -16,10 +16,14 @@ function makeSandbox(options: {
} {
const execCalls: SandboxExecParams[] = [];
const transport = options.transport;
+ const unsupportedOrgSecret = () => Promise.reject(new Error('test provider does not support org secrets'));
const provider: SandboxProvider = {
type: 'test',
envSupported: false,
createSandbox: () => Promise.resolve({ sandboxId: 'test-tenant.sandbox-1' }),
+ createSecret: unsupportedOrgSecret,
+ updateSecret: unsupportedOrgSecret,
+ deleteSecret: unsupportedOrgSecret,
exec: (params): Promise<ExecResult> => {
execCalls.push(params);
return Promise.resolve({ success: true, response: { exitCode: 0, result: '' } });
diff --git a/packages/trueforge-sdk/src/api/resources/sandboxEnvironments/client/Client.ts b/packages/trueforge-sdk/src/api/resources/sandboxEnvironments/client/Client.ts
index 8eabc7088..1ceb2ac6f 100644
--- a/packages/trueforge-sdk/src/api/resources/sandboxEnvironments/client/Client.ts
+++ b/packages/trueforge-sdk/src/api/resources/sandboxEnvironments/client/Client.ts
@@ -151,6 +151,7 @@ export class SandboxEnvironmentsClient {
* @throws {@link TrueForge.UnauthorizedError}
* @throws {@link TrueForge.ConflictError}
* @throws {@link TrueForge.UnprocessableEntityError}
+ * @throws {@link TrueForge.BadGatewayError}
* @throws {@link errors.TrueForgeError}
* @throws {@link errors.TrueForgeTimeoutError}
*
@@ -263,6 +264,17 @@ export class SandboxEnvironmentsClient {
}),
_response.rawResponse,
);
+ case 502:
+ throw new TrueForge.BadGatewayError(
+ serializers.RequestErrorResponse.parseOrThrow(_response.error.body, {
+ unrecognizedObjectKeys: "passthrough",
+ allowUnrecognizedUnionMembers: true,
+ allowUnrecognizedEnumValues: true,
+ skipValidation: true,
+ breadcrumbsPrefix: ["response"],
+ }),
+ _response.rawResponse,
+ );
default:
throw new errors.TrueForgeError({
statusCode: _response.error.statusCode,
@@ -384,6 +396,8 @@ export class SandboxEnvironmentsClient {
* @throws {@link TrueForge.UnauthorizedError}
* @throws {@link TrueForge.NotFoundError}
* @throws {@link TrueForge.ConflictError}
+ * @throws {@link TrueForge.UnprocessableEntityError}
+ * @throws {@link TrueForge.BadGatewayError}
* @throws {@link errors.TrueForgeError}
* @throws {@link errors.TrueForgeTimeoutError}
*
@@ -470,6 +484,28 @@ export class SandboxEnvironmentsClient {
}),
_response.rawResponse,
);
+ case 422:
+ throw new TrueForge.UnprocessableEntityError(
+ serializers.RequestErrorResponse.parseOrThrow(_response.error.body, {
+ unrecognizedObjectKeys: "passthrough",
+ allowUnrecognizedUnionMembers: true,
+ allowUnrecognizedEnumValues: true,
+ skipValidation: true,
+ breadcrumbsPrefix: ["response"],
+ }),
+ _response.rawResponse,
+ );
+ case 502:
+ throw new TrueForge.BadGatewayError(
+ serializers.RequestErrorResponse.parseOrThrow(_response.error.body, {
+ unrecognizedObjectKeys: "passthrough",
+ allowUnrecognizedUnionMembers: true,
+ allowUnrecognizedEnumValues: true,
+ skipValidation: true,
+ breadcrumbsPrefix: ["response"],
+ }),
+ _response.rawResponse,
+ );
default:
throw new errors.TrueForgeError({
statusCode: _response.error.statusCode,
diff --git a/packages/trueforge-sdk/tests/wire/sandboxEnvironments.test.ts b/packages/trueforge-sdk/tests/wire/sandboxEnvironments.test.ts
index 634ee4949..67e838a88 100644
--- a/packages/trueforge-sdk/tests/wire/sandboxEnvironments.test.ts
+++ b/packages/trueforge-sdk/tests/wire/sandboxEnvironments.test.ts
@@ -284,6 +284,30 @@ describe("SandboxEnvironmentsClient", () => {
}).rejects.toThrow(TrueForgeTypes.UnprocessableEntityError);
});
+ test("create_or_update (6)", async () => {
+ const server = mockServerPool.createServer();
+ const client = new TrueForge({ maxRetries: 0, token: "test", baseUrl: server.baseUrl });
+ const rawRequestBody = { manifest: { name: "xy" } };
+ const rawResponseBody = { error: { message: "message" } };
+
+ server
+ .mockEndpoint()
+ .put("/api/v1/sandbox-environments")
+ .jsonBody(rawRequestBody)
+ .respondWith()
+ .statusCode(502)
+ .jsonBody(rawResponseBody)
+ .build();
+
+ await expect(async () => {
+ return await client.sandboxEnvironments.createOrUpdate({
+ manifest: {
+ name: "xy",
+ },
+ });
+ }).rejects.toThrow(TrueForgeTypes.BadGatewayError);
+ });
+
test("get (1)", async () => {
const server = mockServerPool.createServer();
const client = new TrueForge({ maxRetries: 0, token: "test", baseUrl: server.baseUrl });
@@ -463,4 +487,42 @@ describe("SandboxEnvironmentsClient", () => {
return await client.sandboxEnvironments.delete("name");
}).rejects.toThrow(TrueForgeTypes.ConflictError);
});
+
+ test("delete (5)", async () => {
+ const server = mockServerPool.createServer();
+ const client = new TrueForge({ maxRetries: 0, token: "test", baseUrl: server.baseUrl });
+
+ const rawResponseBody = { error: { message: "message" } };
+
+ server
+ .mockEndpoint()
+ .delete("/api/v1/sandbox-environments/name")
+ .respondWith()
+ .statusCode(422)
+ .jsonBody(rawResponseBody)
+ .build();
+
+ await expect(async () => {
+ return await client.sandboxEnvironments.delete("name");
+ }).rejects.toThrow(TrueForgeTypes.UnprocessableEntityError);
+ });
+
+ test("delete (6)", async () => {
+ const server = mockServerPool.createServer();
+ const client = new TrueForge({ maxRetries: 0, token: "test", baseUrl: server.baseUrl });
+
+ const rawResponseBody = { error: { message: "message" } };
+
+ server
+ .mockEndpoint()
+ .delete("/api/v1/sandbox-environments/name")
+ .respondWith()
+ .statusCode(502)
+ .jsonBody(rawResponseBody)
+ .build();
+
+ await expect(async () => {
+ return await client.sandboxEnvironments.delete("name");
+ }).rejects.toThrow(TrueForgeTypes.BadGatewayError);
+ });
});
diff --git a/packages/trueforge/src/apis/capabilities.ts b/packages/trueforge/src/apis/capabilities.ts
index acb4c10b1..70fbb05dc 100644
--- a/packages/trueforge/src/apis/capabilities.ts
+++ b/packages/trueforge/src/apis/capabilities.ts
@@ -32,7 +32,7 @@ export function createCapabilitiesRouter<TTransaction>(deps: {
const router = new OpenAPIHono();
router.openapi(getCapabilitiesRoute, async c => {
const requestContext = deps.resolveRequestContext(c);
- const defaultEnv = await deps.sandboxEnvironmentStore.getEnvironment({
+ const defaultEnv = await deps.sandboxEnvironmentStore.getActiveEnvironment({
tenant_id: requestContext.tenant_id,
name: DEFAULT_SANDBOX_ENVIRONMENT_NAME,
});
diff --git a/packages/trueforge/src/apis/sandboxEnvironments.ts b/packages/trueforge/src/apis/sandboxEnvironments.ts
index e735d485f..785add495 100644
--- a/packages/trueforge/src/apis/sandboxEnvironments.ts
+++ b/packages/trueforge/src/apis/sandboxEnvironments.ts
@@ -1,19 +1,19 @@
/**
* Sandbox environments API (mounted at /api/v1/sandbox-environments).
* Snapshot builds are not started here — versions land in `pending` for a future controller.
+ * Networking secrets sync to Daytona on PUT (plaintext is never persisted).
*/
import { OpenAPIHono, type RouteHandler } from '@hono/zod-openapi';
import { InvalidPageTokenError } from '@truefoundry/trueforge-core/agent-session';
import type { Context } from 'hono';
+import type { Logger } from 'winston';
import { createdBySubjectFromRequestContext, type ResolveRequestContext } from '../auth/identity';
-import { isTrueFoundryModeEnabled } from '../config';
import type { IAgentStore } from '../db/agentStore';
import {
SandboxEnvironmentNameConflictError,
SandboxEnvironmentVersionConflictError,
type ISandboxEnvironmentStore,
type SandboxEnvironmentWithVersion,
- type UpsertSandboxEnvironmentPrevious,
} from '../db/sandboxEnvironmentStore';
import type { ISandboxProviderStore, SandboxProviderRecord } from '../db/sandboxProviderStore';
import {
@@ -22,12 +22,14 @@ import {
listSandboxEnvironmentsRoute,
putSandboxEnvironmentRoute,
} from '../routes/sandboxEnvironmentRoutes';
+import { getDaytonaAuthorizationErrorMessage, toDaytonaSandboxProvider } from '../sandbox/providerUtils';
import { buildNextVersion, redactManifestSecrets } from '../sandbox/sandboxEnvironmentVersion';
import {
- DEFAULT_SANDBOX_ENVIRONMENT_NAME,
- type SandboxEnvironment,
- type SandboxEnvironmentManifest,
-} from '../schemas/sandboxEnvironment';
+ deleteSandboxEnvironmentSecrets,
+ SandboxEnvironmentSecretSyncError,
+ syncSandboxEnvironmentSecrets,
+} from '../sandbox/syncSandboxEnvironmentSecrets';
+import { DEFAULT_SANDBOX_ENVIRONMENT_NAME, type SandboxEnvironment } from '../schemas/sandboxEnvironment';
import { MissingStoredSecretError } from '../utils/secretRedaction';
export interface SandboxEnvironmentsRouterDeps<TTransaction> {
@@ -35,6 +37,7 @@ export interface SandboxEnvironmentsRouterDeps<TTransaction> {
resolveAgentStore: (c: Context) => IAgentStore<TTransaction>;
resolveSandboxProviderStore: (c: Context) => ISandboxProviderStore<TTransaction>;
resolveRequestContext: ResolveRequestContext;
+ logger: Logger;
}
function toSandboxEnvironment({ environment, version }: SandboxEnvironmentWithVersion): SandboxEnvironment {
@@ -63,37 +66,21 @@ async function resolveSandboxProviderRecord(
return providerStore.getSandboxProvider(tenant_id);
}
-function buildUpsertVersion({
- manifest,
- created_by_subject,
- previous,
-}: {
- manifest: SandboxEnvironmentManifest;
- created_by_subject: ReturnType<typeof createdBySubjectFromRequestContext>;
- previous?: UpsertSandboxEnvironmentPrevious;
-}) {
- // Label follows platform mode; create/build always use Daytona credentials + code.
- return {
- ...buildNextVersion({
- version: previous ? previous.latest_version + 1 : 1,
- ...(previous
- ? {
- previous_manifest: previous.previous_manifest,
- previous_external_ref: previous.previous_external_ref,
- }
- : {}),
- manifest,
- provider_type: isTrueFoundryModeEnabled() ? 'truefoundry' : 'daytona',
- }),
- created_by_subject,
- };
+function sandboxEnvironmentSecretHttpError(error: unknown): { status: 422 | 502; message: string } | undefined {
+ if (!(error instanceof SandboxEnvironmentSecretSyncError)) {
+ return undefined;
+ }
+ const authorizationMessage = getDaytonaAuthorizationErrorMessage(error.cause);
+ return authorizationMessage === undefined
+ ? { status: 502, message: error.message }
+ : { status: 422, message: authorizationMessage };
}
/** CRUD for sandbox environments. */
export function createSandboxEnvironmentsRouter<TTransaction>(
deps: SandboxEnvironmentsRouterDeps<TTransaction>,
): OpenAPIHono {
- const { sandboxEnvironmentStore: store, resolveAgentStore, resolveRequestContext } = deps;
+ const { sandboxEnvironmentStore: store, resolveAgentStore, resolveRequestContext, logger } = deps;
const listHandler: RouteHandler<typeof listSandboxEnvironmentsRoute> = async c => {
const { tenant_id, subject } = resolveRequestContext(c);
@@ -142,18 +129,45 @@ export function createSandboxEnvironmentsRouter<TTransaction>(
const { manifest } = body;
try {
+ const existing = await store.getEnvironment({
+ tenant_id: requestContext.tenant_id,
+ name: manifest.name,
+ created_by_subject_id: requestContext.subject.id,
+ });
+ const existingSecrets = existing
+ ? await store.listSecretsByEnvironment({ environment_id: existing.environment.id })
+ : [];
+ const synced_secrets = await syncSandboxEnvironmentSecrets({
+ secrets: manifest.networking?.secrets ?? [],
+ previous: existing?.version.manifest.networking?.secrets ?? [],
+ existing: existingSecrets,
+ provider: toDaytonaSandboxProvider({
+ manifest: provider.manifest,
+ tenant_id: requestContext.tenant_id,
+ logger,
+ }),
+ description: `Secret value of environment ${manifest.name}`,
+ });
+
const result = await store.upsertEnvironment({
tenant_id: requestContext.tenant_id,
name: manifest.name,
description: manifest.description ?? '',
created_by_subject,
- buildVersion: previous =>
- buildUpsertVersion({
- manifest,
+ synced_secrets,
+ buildVersion: ({ existing_version, existing_manifest, existing_external_ref }) =>
+ Promise.resolve({
+ ...buildNextVersion({
+ ...(existing_version !== undefined ? { existing_version } : {}),
+ ...(existing_manifest ? { previous_manifest: existing_manifest } : {}),
+ ...(existing_external_ref ? { previous_external_ref: existing_external_ref } : {}),
+ manifest,
+ provider_type: provider.manifest.type,
+ }),
created_by_subject,
- ...(previous ? { previous } : {}),
}),
});
+
return c.json({ data: toSandboxEnvironment(result) }, 200);
} catch (error) {
if (error instanceof SandboxEnvironmentNameConflictError) {
@@ -165,6 +179,10 @@ export function createSandboxEnvironmentsRouter<TTransaction>(
if (error instanceof MissingStoredSecretError) {
return c.json({ error: { message: 'Secret value is required' } }, 400);
}
+ const secretError = sandboxEnvironmentSecretHttpError(error);
+ if (secretError !== undefined) {
+ return c.json({ error: { message: secretError.message } }, secretError.status);
+ }
throw error;
}
};
@@ -197,12 +215,31 @@ export function createSandboxEnvironmentsRouter<TTransaction>(
409,
);
}
- await store.deleteEnvironment({
- tenant_id,
- name,
- created_by_subject_id: subject.id,
- });
- return c.json({}, 200);
+ try {
+ const secrets = await store.listSecretsByEnvironment({ environment_id: existing.environment.id });
+ if (secrets.length > 0) {
+ const provider = await resolveSandboxProviderRecord(deps.resolveSandboxProviderStore(c), tenant_id);
+ if (provider === undefined) {
+ return c.json({ error: { message: 'No sandbox provider configured' } }, 422);
+ }
+ await deleteSandboxEnvironmentSecrets({
+ secrets,
+ provider: toDaytonaSandboxProvider({ manifest: provider.manifest, tenant_id, logger }),
+ });
+ }
+ await store.deleteEnvironment({
+ tenant_id,
+ name,
+ created_by_subject_id: subject.id,
+ });
+ return c.json({}, 200);
+ } catch (error) {
+ const secretError = sandboxEnvironmentSecretHttpError(error);
+ if (secretError !== undefined) {
+ return c.json({ error: { message: secretError.message } }, secretError.status);
+ }
+ throw error;
+ }
};
const router = new OpenAPIHono();
diff --git a/packages/trueforge/src/app.ts b/packages/trueforge/src/app.ts
index 0f26084dc..97c17b7b3 100644
--- a/packages/trueforge/src/app.ts
+++ b/packages/trueforge/src/app.ts
@@ -372,6 +372,7 @@ export function createServerApp<TTransaction>(deps: ServerDeps<TTransaction>) {
resolveAgentStore: deps.resolveAgentStore,
resolveSandboxProviderStore: deps.resolveSandboxProviderStore,
resolveRequestContext,
+ logger: deps.logger,
}),
authMiddleware,
),
diff --git a/packages/trueforge/src/db/indexes.ts b/packages/trueforge/src/db/indexes.ts
index 69291752c..e69c9757a 100644
--- a/packages/trueforge/src/db/indexes.ts
+++ b/packages/trueforge/src/db/indexes.ts
@@ -36,3 +36,6 @@ export const SANDBOX_ENVIRONMENT_VERSION_UQ = 'sandbox_environment_version_uq';
/** `(tenant_id, created_by_subject.subject_id)` on sandbox_environment. */
export const SANDBOX_ENVIRONMENT_CREATED_BY_SUBJECT_ID_IDX = 'sandbox_environment_created_by_subject_id_idx';
+
+/** Unique `(environment_id, secret_name)` on sandbox_environment_secret. */
+export const SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ = 'sandbox_environment_secret_environment_id_secret_name_uq';
diff --git a/packages/trueforge/src/db/postgres/migrations/20260930_000001_sandbox_environment_secret.ts b/packages/trueforge/src/db/postgres/migrations/20260930_000001_sandbox_environment_secret.ts
new file mode 100644
index 000000000..d58272b2b
--- /dev/null
+++ b/packages/trueforge/src/db/postgres/migrations/20260930_000001_sandbox_environment_secret.ts
@@ -0,0 +1,35 @@
+import { sql, type Kysely } from 'kysely';
+import { SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ } from '../../indexes';
+
+/** Per-environment Daytona secret refs (Postgres). */
+export async function up(db: Kysely<unknown>): Promise<void> {
+ await sql`SET LOCAL lock_timeout = '5s'`.execute(db);
+
+ await db.schema
+ .createTable('sandbox_environment_secret')
+ .addColumn('id', 'text', col => col.notNull())
+ .addColumn('tenant_id', 'text', col => col.notNull())
+ .addColumn('environment_id', 'text', col => col.notNull())
+ .addColumn('secret_name', 'text', col => col.notNull())
+ .addColumn('external_secret_name', 'text', col => col.notNull())
+ .addColumn('external_secret_id', 'text', col => col.notNull())
+ .addColumn('description', 'text', col => col.notNull())
+ .addColumn('hash', 'text', col => col.notNull())
+ .addColumn('created_at', 'timestamptz', col => col.notNull())
+ .addColumn('updated_at', 'timestamptz', col => col.notNull())
+ .addPrimaryKeyConstraint('sandbox_environment_secret_pkey', ['id'])
+ .addUniqueConstraint(SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ, ['environment_id', 'secret_name'])
+ .addForeignKeyConstraint(
+ 'sandbox_environment_secret_environment_id_fkey',
+ ['environment_id'],
+ 'sandbox_environment',
+ ['id'],
+ cb => cb.onDelete('cascade'),
+ )
+ .execute();
+}
+
+export async function down(db: Kysely<unknown>): Promise<void> {
+ await sql`SET LOCAL lock_timeout = '5s'`.execute(db);
+ await db.schema.dropTable('sandbox_environment_secret').ifExists().execute();
+}
diff --git a/packages/trueforge/src/db/postgres/sandbox-environment-store/PostgresSandboxEnvironmentStore.ts b/packages/trueforge/src/db/postgres/sandbox-environment-store/PostgresSandboxEnvironmentStore.ts
index 6a5c7bf3c..55ac67eb1 100644
--- a/packages/trueforge/src/db/postgres/sandbox-environment-store/PostgresSandboxEnvironmentStore.ts
+++ b/packages/trueforge/src/db/postgres/sandbox-environment-store/PostgresSandboxEnvironmentStore.ts
@@ -11,12 +11,12 @@ import {
SandboxEnvironmentVersionInternalMetadataSchema,
} from '../../../schemas/sandboxEnvironment';
import { newId } from '../../../utils/id';
+import { hashSandboxEnvironmentSecret, isRedactedSecretValue, SECRET_REDACTION } from '../../../utils/secretRedaction';
import { SANDBOX_ENVIRONMENT_TENANT_NAME_ACTIVE_UQ, SANDBOX_ENVIRONMENT_VERSION_UQ } from '../../indexes';
import {
+ parseStoredSandboxEnvironmentManifest,
SandboxEnvironmentNameConflictError,
SandboxEnvironmentVersionConflictError,
- parseStoredSandboxEnvironmentManifest,
- toUpsertSandboxEnvironmentVersionWrite,
type DeleteSandboxEnvironmentInput,
type GetSandboxEnvironmentInput,
type GetSandboxEnvironmentVersionInput,
@@ -25,6 +25,7 @@ import {
type MarkSandboxEnvironmentVersionFailedInput,
type MarkSandboxEnvironmentVersionReadyInput,
type SandboxEnvironmentRecord,
+ type SandboxEnvironmentSecretRecord,
type SandboxEnvironmentVersionForProgress,
type SandboxEnvironmentVersionRecord,
type SandboxEnvironmentWithVersion,
@@ -33,7 +34,27 @@ import {
} from '../../sandboxEnvironmentStore';
import { isPgConstraint, isUniqueViolation } from '../client';
import { json, now } from '../sqlExpressions';
-import type { Database, SandboxEnvironmentTable, SandboxEnvironmentVersionTable } from '../types';
+import type {
+ Database,
+ SandboxEnvironmentSecretTable,
+ SandboxEnvironmentTable,
+ SandboxEnvironmentVersionTable,
+} from '../types';
+
+function toSecretRecord(row: Selectable<SandboxEnvironmentSecretTable>): SandboxEnvironmentSecretRecord {
+ return {
+ id: row.id,
+ tenant_id: row.tenant_id,
+ environment_id: row.environment_id,
+ secret_name: row.secret_name,
+ external_secret_name: row.external_secret_name,
+ external_secret_id: row.external_secret_id,
+ description: row.description,
+ hash: row.hash,
+ created_at: row.created_at.toISOString(),
+ updated_at: row.updated_at.toISOString(),
+ };
+}
type JoinedRow = Selectable<SandboxEnvironmentTable> & {
ver_id: string;
@@ -120,6 +141,33 @@ function activeVersionJoin(db: Kysely<Database> | Transaction<Database>) {
]);
}
+function latestVersionJoin(db: Kysely<Database> | Transaction<Database>) {
+ return db
+ .selectFrom('sandbox_environment as env')
+ .innerJoin('sandbox_environment_version as ver', 'ver.environment_id', 'env.id')
+ .selectAll('env')
+ .select([
+ 'ver.id as ver_id',
+ 'ver.environment_id as ver_environment_id',
+ 'ver.version as ver_version',
+ 'ver.manifest as ver_manifest',
+ 'ver.status as ver_status',
+ 'ver.status_reason as ver_status_reason',
+ 'ver.external_ref as ver_external_ref',
+ 'ver.internal_metadata as ver_internal_metadata',
+ 'ver.created_by_subject as ver_created_by_subject',
+ 'ver.created_at as ver_created_at',
+ 'ver.updated_at as ver_updated_at',
+ ])
+ .where(
+ sql<boolean>`ver.version = (
+ SELECT MAX(latest.version)
+ FROM sandbox_environment_version AS latest
+ WHERE latest.environment_id = env.id
+ )`,
+ );
+}
+
export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore<Transaction<Database>> {
readonly #db: Kysely<Database>;
@@ -132,7 +180,7 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
transaction?: Transaction<Database>,
): Promise<{ data: SandboxEnvironmentWithVersion[]; pagination: TokenPagination }> {
const db = transaction ?? this.#db;
- const query = activeVersionJoin(db)
+ const query = latestVersionJoin(db)
.where('env.tenant_id', '=', input.tenant_id)
.where('env.lifecycle_stage', '=', 'active')
.where(eb =>
@@ -161,6 +209,25 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
async getEnvironment(
input: GetSandboxEnvironmentInput,
transaction?: Transaction<Database>,
+ ): Promise<SandboxEnvironmentWithVersion | undefined> {
+ const db = transaction ?? this.#db;
+ let query = latestVersionJoin(db)
+ .where('env.tenant_id', '=', input.tenant_id)
+ .where('env.name', '=', input.name)
+ .where('env.lifecycle_stage', '=', 'active');
+ if (input.created_by_subject_id && input.name !== DEFAULT_SANDBOX_ENVIRONMENT_NAME) {
+ query = query.where(sql`env.created_by_subject->>'subject_id'`, '=', input.created_by_subject_id);
+ }
+ const row = await query.executeTakeFirst();
+ if (!row) {
+ return undefined;
+ }
+ return this.#withMountedSecrets(toWithVersion(row), db);
+ }
+
+ async getActiveEnvironment(
+ input: GetSandboxEnvironmentInput,
+ transaction?: Transaction<Database>,
): Promise<SandboxEnvironmentWithVersion | undefined> {
const db = transaction ?? this.#db;
let query = activeVersionJoin(db)
@@ -171,7 +238,10 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
query = query.where(sql`env.created_by_subject->>'subject_id'`, '=', input.created_by_subject_id);
}
const row = await query.executeTakeFirst();
- return row ? toWithVersion(row) : undefined;
+ if (!row) {
+ return undefined;
+ }
+ return this.#withMountedSecrets(toWithVersion(row), db);
}
async listLatestPendingVersions(transaction?: Transaction<Database>): Promise<string[]> {
@@ -213,6 +283,7 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
'ver.version',
'ver.external_ref',
'ver.manifest',
+ 'ver.internal_metadata',
])
.where('ver.id', '=', input.environment_version_id)
.where('env.lifecycle_stage', '=', 'active')
@@ -228,6 +299,7 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
version: row.version,
external_ref: row.external_ref,
manifest: parseStoredSandboxEnvironmentManifest(row.manifest),
+ internal_metadata: SandboxEnvironmentVersionInternalMetadataSchema.parse(row.internal_metadata),
};
}
@@ -263,10 +335,7 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
if (!environmentRow) {
const environment_id = newId();
- const versionWrite = toUpsertSandboxEnvironmentVersionWrite(input.buildVersion());
- // First version: point here so get/list join works; later versions only move the
- // pointer when their status is (or becomes) ready.
- const active_version = versionWrite.version;
+ // Parent first so secret FKs can resolve in this transaction.
try {
const created = await db
.insertInto('sandbox_environment')
@@ -275,7 +344,7 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
tenant_id: input.tenant_id,
name: input.name,
description: input.description,
- active_version,
+ active_version: 1,
lifecycle_stage: 'active',
created_by_subject: json(input.created_by_subject),
created_at: now(),
@@ -283,8 +352,19 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
})
.returningAll()
.executeTakeFirstOrThrow();
+ const versionWrite = await this.#buildVersion({
+ input,
+ environment_id,
+ db,
+ });
const version = await this.#insertVersionRow(db, environment_id, versionWrite);
- return { environment: toEnvironmentRecord(created), version };
+ return {
+ environment: toEnvironmentRecord({
+ ...created,
+ active_version: versionWrite.version,
+ }),
+ version,
+ };
} catch (error) {
if (isUniqueViolation(error) || isPgConstraint(error, SANDBOX_ENVIRONMENT_TENANT_NAME_ACTIVE_UQ)) {
throw new SandboxEnvironmentNameConflictError(
@@ -302,13 +382,14 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
.where('environment_id', '=', environmentRow.id)
.orderBy('version', 'desc')
.executeTakeFirstOrThrow();
- const versionWrite = toUpsertSandboxEnvironmentVersionWrite(
- input.buildVersion({
- latest_version: previousVersion.version,
- previous_manifest: parseStoredSandboxEnvironmentManifest(previousVersion.manifest),
- previous_external_ref: previousVersion.external_ref,
- }),
- );
+ const versionWrite = await this.#buildVersion({
+ input,
+ environment_id: environmentRow.id,
+ db,
+ existing_version: previousVersion.version,
+ existing_manifest: parseStoredSandboxEnvironmentManifest(previousVersion.manifest),
+ existing_external_ref: previousVersion.external_ref,
+ });
const version = await this.#insertVersionRow(db, environmentRow.id, versionWrite);
const updated = await db
@@ -398,17 +479,33 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
}
async deleteEnvironment(input: DeleteSandboxEnvironmentInput, transaction?: Transaction<Database>): Promise<void> {
- const db = transaction ?? this.#db;
+ if (transaction) {
+ return this.#deleteEnvironment(input, transaction);
+ }
+ return this.#db.transaction().execute(db => this.#deleteEnvironment(input, db));
+ }
+
+ async #deleteEnvironment(input: DeleteSandboxEnvironmentInput, db: Transaction<Database>): Promise<void> {
+ const environment = await db
+ .selectFrom('sandbox_environment')
+ .select('id')
+ .where('tenant_id', '=', input.tenant_id)
+ .where('name', '=', input.name)
+ .where('lifecycle_stage', '=', 'active')
+ .where(sql`created_by_subject->>'subject_id'`, '=', input.created_by_subject_id)
+ .forUpdate()
+ .executeTakeFirst();
+ if (environment === undefined) {
+ return;
+ }
+ await db.deleteFrom('sandbox_environment_secret').where('environment_id', '=', environment.id).execute();
await db
.updateTable('sandbox_environment')
.set({
lifecycle_stage: 'deleted',
updated_at: now(),
})
- .where('tenant_id', '=', input.tenant_id)
- .where('name', '=', input.name)
- .where('lifecycle_stage', '=', 'active')
- .where(sql`created_by_subject->>'subject_id'`, '=', input.created_by_subject_id)
+ .where('id', '=', environment.id)
.execute();
}
@@ -446,4 +543,137 @@ export class PostgresSandboxEnvironmentStore implements ISandboxEnvironmentStore
throw error;
}
}
+
+ async #buildVersion({
+ input,
+ environment_id,
+ db,
+ existing_version,
+ existing_manifest,
+ existing_external_ref,
+ }: {
+ input: UpsertSandboxEnvironmentInput;
+ environment_id: string;
+ db: Transaction<Database>;
+ existing_version?: number;
+ existing_manifest?: ReturnType<typeof parseStoredSandboxEnvironmentManifest>;
+ existing_external_ref?: string;
+ }): Promise<UpsertSandboxEnvironmentVersionWrite> {
+ const built = await input.buildVersion({
+ environment_id,
+ ...(existing_version ? { existing_version } : {}),
+ ...(existing_manifest ? { existing_manifest } : {}),
+ ...(existing_external_ref ? { existing_external_ref } : {}),
+ });
+ const secretDescription = `Secret value of environment ${built.manifest.name}`;
+ const existingSecretByName = new Map(
+ (await this.listSecretsByEnvironment({ environment_id }, db)).map(row => [row.secret_name, row]),
+ );
+ const syncedByName = new Map(input.synced_secrets.map(secret => [secret.secret_name, secret]));
+ const desiredNames = new Set((built.manifest.networking?.secrets ?? []).map(secret => secret.env));
+ const secrets: { key: string; id: string }[] = [];
+ for (const secret of built.manifest.networking?.secrets ?? []) {
+ const existing = existingSecretByName.get(secret.env);
+ const synced = syncedByName.get(secret.env);
+ if (synced === undefined) {
+ throw new Error(`Synced sandbox environment secret missing for ${secret.env}`);
+ }
+ const hash = !isRedactedSecretValue(secret.value)
+ ? hashSandboxEnvironmentSecret({ tenant_id: input.tenant_id, value: secret.value })
+ : existing?.hash;
+ if (!hash) {
+ throw new Error(`Sandbox environment secret hash missing for ${secret.env}`);
+ }
+ const timestamp = now();
+ const row = await db
+ .insertInto('sandbox_environment_secret')
+ .values({
+ id: newId(),
+ tenant_id: input.tenant_id,
+ environment_id,
+ secret_name: secret.env,
+ external_secret_name: synced.external_secret_name,
+ external_secret_id: synced.external_secret_id,
+ description: secretDescription,
+ hash,
+ created_at: timestamp,
+ updated_at: timestamp,
+ })
+ .onConflict(oc =>
+ oc.columns(['environment_id', 'secret_name']).doUpdateSet({
+ external_secret_name: synced.external_secret_name,
+ external_secret_id: synced.external_secret_id,
+ description: secretDescription,
+ hash,
+ updated_at: timestamp,
+ }),
+ )
+ .returning('id')
+ .executeTakeFirstOrThrow();
+ secrets.push({ key: secret.env, id: row.id });
+ }
+ const removedIds = [...existingSecretByName.values()]
+ .filter(row => !desiredNames.has(row.secret_name))
+ .map(row => row.id);
+ if (removedIds.length > 0) {
+ await db.deleteFrom('sandbox_environment_secret').where('id', 'in', removedIds).execute();
+ }
+ const networking = built.manifest.networking;
+ return {
+ ...built,
+ manifest: {
+ ...built.manifest,
+ ...(networking?.secrets
+ ? {
+ networking: {
+ ...networking,
+ secrets: networking.secrets.map(secret => ({ ...secret, value: SECRET_REDACTION })),
+ },
+ }
+ : {}),
+ },
+ internal_metadata: { secrets },
+ };
+ }
+
+ async listSecretsByEnvironment(
+ input: { environment_id: string },
+ transaction?: Transaction<Database>,
+ ): Promise<SandboxEnvironmentSecretRecord[]> {
+ const db = transaction ?? this.#db;
+ const rows = await db
+ .selectFrom('sandbox_environment_secret')
+ .selectAll()
+ .where('environment_id', '=', input.environment_id)
+ .execute();
+ return rows.map(toSecretRecord);
+ }
+
+ async #withMountedSecrets(
+ loaded: SandboxEnvironmentWithVersion,
+ db: Kysely<Database> | Transaction<Database>,
+ ): Promise<SandboxEnvironmentWithVersion> {
+ const refs = loaded.version.internal_metadata.secrets;
+ if (refs.length === 0) {
+ return loaded;
+ }
+ const rows = await db
+ .selectFrom('sandbox_environment_secret')
+ .selectAll()
+ .where(
+ 'id',
+ 'in',
+ refs.map(ref => ref.id),
+ )
+ .execute();
+ const byId = new Map(rows.map(row => [row.id, row]));
+ const mounted_secrets: Record<string, string> = {};
+ for (const ref of refs) {
+ const row = byId.get(ref.id);
+ if (row !== undefined) {
+ mounted_secrets[ref.key] = row.external_secret_name;
+ }
+ }
+ return Object.keys(mounted_secrets).length > 0 ? { ...loaded, mounted_secrets } : loaded;
+ }
}
diff --git a/packages/trueforge/src/db/postgres/types.ts b/packages/trueforge/src/db/postgres/types.ts
index 20169e9af..65e2c877f 100644
--- a/packages/trueforge/src/db/postgres/types.ts
+++ b/packages/trueforge/src/db/postgres/types.ts
@@ -460,6 +460,23 @@ export interface SandboxEnvironmentVersionTable {
updated_at: Date;
}
+/**
+ * Per-environment Daytona org secret refs (no plaintext value).
+ * UNIQUE (environment_id, secret_name); FK → sandbox_environment ON DELETE CASCADE.
+ */
+export interface SandboxEnvironmentSecretTable {
+ id: string;
+ tenant_id: string;
+ environment_id: string;
+ secret_name: string;
+ external_secret_name: string;
+ external_secret_id: string;
+ description: string;
+ hash: string;
+ created_at: Date;
+ updated_at: Date;
+}
+
/**
* Configured schedules — immutable ULID `id` PK.
* PRIMARY KEY (id)
@@ -596,6 +613,7 @@ export interface Database {
agent: AgentTable;
sandbox_environment: SandboxEnvironmentTable;
sandbox_environment_version: SandboxEnvironmentVersionTable;
+ sandbox_environment_secret: SandboxEnvironmentSecretTable;
schedule: ScheduleTable;
schedule_run: ScheduleRunTable;
mcp_server: McpServerTable;
diff --git a/packages/trueforge/src/db/sandboxEnvironmentStore.ts b/packages/trueforge/src/db/sandboxEnvironmentStore.ts
index 8add362d5..bafd4023d 100644
--- a/packages/trueforge/src/db/sandboxEnvironmentStore.ts
+++ b/packages/trueforge/src/db/sandboxEnvironmentStore.ts
@@ -47,6 +47,28 @@ export interface SandboxEnvironmentVersionRecord {
export interface SandboxEnvironmentWithVersion {
environment: SandboxEnvironmentRecord;
version: SandboxEnvironmentVersionRecord;
+ /** Env var → Daytona org secret name (from getEnvironment). */
+ mounted_secrets?: Record<string, string>;
+}
+
+/** Secret row (Daytona refs; no plaintext). */
+export interface SandboxEnvironmentSecretRecord {
+ id: string;
+ tenant_id: string;
+ environment_id: string;
+ secret_name: string;
+ external_secret_name: string;
+ external_secret_id: string;
+ description: string;
+ hash: string;
+ created_at: string;
+ updated_at: string;
+}
+
+export interface SyncedSandboxEnvironmentSecret {
+ secret_name: string;
+ external_secret_name: string;
+ external_secret_id: string;
}
export function parseStoredSandboxEnvironmentManifest(manifest: unknown): StoredSandboxEnvironmentManifest {
@@ -80,6 +102,7 @@ export interface SandboxEnvironmentVersionForProgress {
version: number;
external_ref: string;
manifest: StoredSandboxEnvironmentManifest;
+ internal_metadata: SandboxEnvironmentVersionInternalMetadata;
}
export interface GetSandboxEnvironmentVersionInput {
@@ -93,20 +116,17 @@ export type UpsertSandboxEnvironmentVersion = NextSandboxEnvironmentVersion & {
created_by_subject: CreatedBySubject;
};
-/** Version row columns for insert (store fills `environment_id`). */
-export type UpsertSandboxEnvironmentVersionWrite = UpsertSandboxEnvironmentVersion;
-
-export function toUpsertSandboxEnvironmentVersionWrite(
- built: UpsertSandboxEnvironmentVersion,
-): UpsertSandboxEnvironmentVersionWrite {
- return built;
-}
+/** Complete version row fields after the store resolves secret-row references. */
+export type UpsertSandboxEnvironmentVersionWrite = UpsertSandboxEnvironmentVersion & {
+ internal_metadata: SandboxEnvironmentVersionInternalMetadata;
+};
-/** Latest version row when updating; omitted on first create. Used for numbering + secret/diff. */
-export interface UpsertSandboxEnvironmentPrevious {
- latest_version: number;
- previous_manifest: StoredSandboxEnvironmentManifest;
- previous_external_ref: string;
+/** Args for `buildVersion` during upsert. Tip fields omitted on first create. */
+export interface ExistingSandboxEnvironmentVersion {
+ environment_id: string;
+ existing_version?: number;
+ existing_manifest?: StoredSandboxEnvironmentManifest;
+ existing_external_ref?: string;
}
export interface UpsertSandboxEnvironmentInput {
@@ -114,12 +134,10 @@ export interface UpsertSandboxEnvironmentInput {
name: ResourceName;
description: string;
created_by_subject: CreatedBySubject;
- /**
- * Called inside the write transaction. `previous` is set when updating an existing
- * env (after the parent row is locked / re-read) so concurrent PUTs cannot collide
- * on the next version number.
- */
- buildVersion: (previous?: UpsertSandboxEnvironmentPrevious) => UpsertSandboxEnvironmentVersion;
+ /** Complete provider refs from a successful secret sync before upsert. */
+ synced_secrets: SyncedSandboxEnvironmentSecret[];
+ /** Called after parent lock/create; store upserts secrets from the returned manifest. */
+ buildVersion: (input: ExistingSandboxEnvironmentVersion) => Promise<UpsertSandboxEnvironmentVersion>;
}
export interface MarkSandboxEnvironmentVersionReadyInput {
@@ -171,14 +189,19 @@ export interface ISandboxEnvironmentStore<TTransaction = never> {
transaction?: TTransaction,
): Promise<{ data: SandboxEnvironmentWithVersion[]; pagination: TokenPagination }>;
/**
- * Active environment by name, joined to its active version.
+ * Active environment by name, joined to its latest version for management APIs.
* Pass `created_by_subject_id` for owner-scoped CRUD/attach on custom envs; the tenant
- * `"default"` ignores ownership. Omit the subject filter when an agent run resolves an env.
+ * `"default"` ignores ownership.
*/
getEnvironment(
input: GetSandboxEnvironmentInput,
transaction?: TTransaction,
): Promise<SandboxEnvironmentWithVersion | undefined>;
+ /** Active environment by name, joined to the version currently used for sandbox creation. */
+ getActiveEnvironment(
+ input: GetSandboxEnvironmentInput,
+ transaction?: TTransaction,
+ ): Promise<SandboxEnvironmentWithVersion | undefined>;
/**
* Create or replace by `(tenant_id, name)` — parent + version row.
* Updates insert a new version; parent `active_version` advances only when status is
@@ -210,4 +233,8 @@ export interface ISandboxEnvironmentStore<TTransaction = never> {
): Promise<SandboxEnvironmentVersionRecord | undefined>;
/** Soft-delete: set lifecycle_stage = deleted. Idempotent if missing or already deleted. */
deleteEnvironment(input: DeleteSandboxEnvironmentInput, transaction?: TTransaction): Promise<void>;
+ listSecretsByEnvironment(
+ input: { environment_id: string },
+ transaction?: TTransaction,
+ ): Promise<SandboxEnvironmentSecretRecord[]>;
}
diff --git a/packages/trueforge/src/db/sqlite/migrations/20260930_000001_sandbox_environment_secret.ts b/packages/trueforge/src/db/sqlite/migrations/20260930_000001_sandbox_environment_secret.ts
new file mode 100644
index 000000000..1a97dd74d
--- /dev/null
+++ b/packages/trueforge/src/db/sqlite/migrations/20260930_000001_sandbox_environment_secret.ts
@@ -0,0 +1,41 @@
+import { sql, type Kysely } from 'kysely';
+import { SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ } from '../../indexes';
+
+/** Per-environment Daytona secret refs (SQLite). */
+export async function up(db: Kysely<unknown>): Promise<void> {
+ await db.transaction().execute(async trx => {
+ await sql`
+ CREATE TABLE sandbox_environment_secret (
+ id TEXT NOT NULL,
+ tenant_id TEXT NOT NULL,
+ environment_id TEXT NOT NULL,
+ secret_name TEXT NOT NULL,
+ external_secret_name TEXT NOT NULL,
+ external_secret_id TEXT NOT NULL,
+ description TEXT NOT NULL,
+ hash TEXT NOT NULL,
+ created_at TEXT NOT NULL,
+ updated_at TEXT NOT NULL,
+ PRIMARY KEY (id),
+ FOREIGN KEY (environment_id) REFERENCES sandbox_environment (id) ON DELETE CASCADE
+ ) STRICT
+ `.execute(trx);
+
+ await sql`
+ CREATE UNIQUE INDEX ${sql.raw(SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ)}
+ ON sandbox_environment_secret (environment_id, secret_name)
+ `.execute(trx);
+ });
+}
+
+export async function down(db: Kysely<unknown>): Promise<void> {
+ await sql`PRAGMA foreign_keys = OFF`.execute(db);
+ try {
+ await db.transaction().execute(async trx => {
+ await sql`DROP INDEX IF EXISTS ${sql.raw(SANDBOX_ENVIRONMENT_SECRET_ENV_NAME_UQ)}`.execute(trx);
+ await sql`DROP TABLE IF EXISTS sandbox_environment_secret`.execute(trx);
+ });
+ } finally {
+ await sql`PRAGMA foreign_keys = ON`.execute(db);
+ }
+}
diff --git a/packages/trueforge/src/db/sqlite/sandbox-environment-store/SqliteSandboxEnvironmentStore.ts b/packages/trueforge/src/db/sqlite/sandbox-environment-store/SqliteSandboxEnvironmentStore.ts
index 036805e00..df854e4cc 100644
--- a/packages/trueforge/src/db/sqlite/sandbox-environment-store/SqliteSandboxEnvironmentStore.ts
+++ b/packages/trueforge/src/db/sqlite/sandbox-environment-store/SqliteSandboxEnvironmentStore.ts
@@ -9,7 +9,6 @@ import {
} from '@truefoundry/trueforge-core/agent-session/store/OffsetPageToken';
import type { Kysely, Transaction } from 'kysely';
import { sql } from 'kysely';
-import {} from '../../../sandbox/sandboxEnvironmentVersion';
import { NameSchema } from '../../../schemas/common';
import {
DEFAULT_SANDBOX_ENVIRONMENT_NAME,
@@ -18,11 +17,11 @@ import {
type StoredSandboxEnvironmentManifest,
} from '../../../schemas/sandboxEnvironment';
import { newId } from '../../../utils/id';
+import { hashSandboxEnvironmentSecret, isRedactedSecretValue, SECRET_REDACTION } from '../../../utils/secretRedaction';
import {
parseStoredSandboxEnvironmentManifest,
SandboxEnvironmentNameConflictError,
SandboxEnvironmentVersionConflictError,
- toUpsertSandboxEnvironmentVersionWrite,
type DeleteSandboxEnvironmentInput,
type GetSandboxEnvironmentInput,
type GetSandboxEnvironmentVersionInput,
@@ -31,6 +30,7 @@ import {
type MarkSandboxEnvironmentVersionFailedInput,
type MarkSandboxEnvironmentVersionReadyInput,
type SandboxEnvironmentRecord,
+ type SandboxEnvironmentSecretRecord,
type SandboxEnvironmentVersionForProgress,
type SandboxEnvironmentVersionRecord,
type SandboxEnvironmentWithVersion,
@@ -41,6 +41,32 @@ import { isUniqueViolation } from '../client';
import { jsonbBind, jsonText, nowIso } from '../sqlExpressions';
import type { Database } from '../types';
+function toSecretRecord(row: {
+ id: string;
+ tenant_id: string;
+ environment_id: string;
+ secret_name: string;
+ external_secret_name: string;
+ external_secret_id: string;
+ description: string;
+ hash: string;
+ created_at: string;
+ updated_at: string;
+}): SandboxEnvironmentSecretRecord {
+ return {
+ id: row.id,
+ tenant_id: row.tenant_id,
+ environment_id: row.environment_id,
+ secret_name: row.secret_name,
+ external_secret_name: row.external_secret_name,
+ external_secret_id: row.external_secret_id,
+ description: row.description,
+ hash: row.hash,
+ created_at: row.created_at,
+ updated_at: row.updated_at,
+ };
+}
+
function activeVersionJoin(db: Kysely<Database> | Transaction<Database>) {
return db
.selectFrom('sandbox_environment as env')
@@ -71,6 +97,41 @@ function activeVersionJoin(db: Kysely<Database> | Transaction<Database>) {
]);
}
+function latestVersionJoin(db: Kysely<Database> | Transaction<Database>) {
+ return db
+ .selectFrom('sandbox_environment as env')
+ .innerJoin('sandbox_environment_version as ver', 'ver.environment_id', 'env.id')
+ .select([
+ 'env.id',
+ 'env.tenant_id',
+ 'env.name',
+ 'env.description',
+ 'env.active_version',
+ 'env.lifecycle_stage',
+ jsonText<CreatedBySubject>(sql.ref('env.created_by_subject')).as('created_by_subject'),
+ 'env.created_at',
+ 'env.updated_at',
+ 'ver.id as ver_id',
+ 'ver.environment_id as ver_environment_id',
+ 'ver.version as ver_version',
+ jsonText<StoredSandboxEnvironmentManifest>(sql.ref('ver.manifest')).as('ver_manifest'),
+ 'ver.status as ver_status',
+ 'ver.status_reason as ver_status_reason',
+ 'ver.external_ref as ver_external_ref',
+ jsonText<SandboxEnvironmentVersionInternalMetadata>(sql.ref('ver.internal_metadata')).as('ver_internal_metadata'),
+ jsonText<CreatedBySubject>(sql.ref('ver.created_by_subject')).as('ver_created_by_subject'),
+ 'ver.created_at as ver_created_at',
+ 'ver.updated_at as ver_updated_at',
+ ])
+ .where(
+ sql<boolean>`ver.version = (
+ SELECT MAX(latest.version)
+ FROM sandbox_environment_version AS latest
+ WHERE latest.environment_id = env.id
+ )`,
+ );
+}
+
function versionSelect() {
return [
'id' as const,
@@ -191,7 +252,7 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
transaction?: Transaction<Database>,
): Promise<{ data: SandboxEnvironmentWithVersion[]; pagination: TokenPagination }> {
const db = transaction ?? this.#db;
- const query = activeVersionJoin(db)
+ const query = latestVersionJoin(db)
.where('env.tenant_id', '=', input.tenant_id)
.where('env.lifecycle_stage', '=', 'active')
.where(eb =>
@@ -220,6 +281,25 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
async getEnvironment(
input: GetSandboxEnvironmentInput,
transaction?: Transaction<Database>,
+ ): Promise<SandboxEnvironmentWithVersion | undefined> {
+ const db = transaction ?? this.#db;
+ let query = latestVersionJoin(db)
+ .where('env.tenant_id', '=', input.tenant_id)
+ .where('env.name', '=', input.name)
+ .where('env.lifecycle_stage', '=', 'active');
+ if (input.created_by_subject_id && input.name !== DEFAULT_SANDBOX_ENVIRONMENT_NAME) {
+ query = query.where(sql`json_extract(env.created_by_subject, '$.subject_id')`, '=', input.created_by_subject_id);
+ }
+ const row = await query.executeTakeFirst();
+ if (!row) {
+ return undefined;
+ }
+ return this.#withMountedSecrets(toWithVersion(row), db);
+ }
+
+ async getActiveEnvironment(
+ input: GetSandboxEnvironmentInput,
+ transaction?: Transaction<Database>,
): Promise<SandboxEnvironmentWithVersion | undefined> {
const db = transaction ?? this.#db;
let query = activeVersionJoin(db)
@@ -230,7 +310,10 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
query = query.where(sql`json_extract(env.created_by_subject, '$.subject_id')`, '=', input.created_by_subject_id);
}
const row = await query.executeTakeFirst();
- return row ? toWithVersion(row) : undefined;
+ if (!row) {
+ return undefined;
+ }
+ return this.#withMountedSecrets(toWithVersion(row), db);
}
async listLatestPendingVersions(transaction?: Transaction<Database>): Promise<string[]> {
@@ -271,6 +354,7 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
'ver.version',
'ver.external_ref',
jsonText<StoredSandboxEnvironmentManifest>(sql.ref('ver.manifest')).as('manifest'),
+ jsonText<SandboxEnvironmentVersionInternalMetadata>(sql.ref('ver.internal_metadata')).as('internal_metadata'),
])
.where('ver.id', '=', input.environment_version_id)
.where('env.lifecycle_stage', '=', 'active')
@@ -286,6 +370,7 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
version: row.version,
external_ref: row.external_ref,
manifest: parseStoredSandboxEnvironmentManifest(row.manifest),
+ internal_metadata: SandboxEnvironmentVersionInternalMetadataSchema.parse(row.internal_metadata),
};
}
@@ -331,11 +416,8 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
if (!environmentRow) {
const environment_id = newId();
- const versionWrite = toUpsertSandboxEnvironmentVersionWrite(input.buildVersion());
const created_at = nowIso();
- // First version: point here so get/list join works; later versions only move the
- // pointer when their status is (or becomes) ready.
- const active_version = versionWrite.version;
+ // Parent first so secret FKs can resolve in this transaction.
try {
await db
.insertInto('sandbox_environment')
@@ -344,7 +426,7 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
tenant_id: input.tenant_id,
name: input.name,
description: input.description,
- active_version,
+ active_version: 1,
lifecycle_stage: 'active',
created_by_subject: jsonbBind(input.created_by_subject),
created_at,
@@ -360,6 +442,11 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
}
throw error;
}
+ const versionWrite = await this.#buildVersion({
+ input,
+ environment_id,
+ db,
+ });
const version = await this.#insertVersionRow(db, environment_id, versionWrite);
return {
environment: {
@@ -367,7 +454,7 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
tenant_id: input.tenant_id,
name: input.name,
description: input.description,
- active_version,
+ active_version: versionWrite.version,
lifecycle_stage: 'active',
created_by_subject: input.created_by_subject,
created_at,
@@ -383,13 +470,14 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
.where('environment_id', '=', environmentRow.id)
.orderBy('version', 'desc')
.executeTakeFirstOrThrow();
- const versionWrite = toUpsertSandboxEnvironmentVersionWrite(
- input.buildVersion({
- latest_version: previousVersion.version,
- previous_manifest: parseStoredSandboxEnvironmentManifest(previousVersion.manifest),
- previous_external_ref: previousVersion.external_ref,
- }),
- );
+ const versionWrite = await this.#buildVersion({
+ input,
+ environment_id: environmentRow.id,
+ db,
+ existing_version: previousVersion.version,
+ existing_manifest: parseStoredSandboxEnvironmentManifest(previousVersion.manifest),
+ existing_external_ref: previousVersion.external_ref,
+ });
const updated_at = nowIso();
const version = await this.#insertVersionRow(db, environmentRow.id, versionWrite);
@@ -497,18 +585,166 @@ export class SqliteSandboxEnvironmentStore implements ISandboxEnvironmentStore<T
}
async deleteEnvironment(input: DeleteSandboxEnvironmentInput, transaction?: Transaction<Database>): Promise<void> {
- const db = transaction ?? this.#db;
+ if (transaction) {
+ return this.#deleteEnvironment(input, transaction);
+ }
+ return this.#db.transaction().execute(db => this.#deleteEnvironment(input, db));
+ }
+
+ async #deleteEnvironment(input: DeleteSandboxEnvironmentInput, db: Transaction<Database>): Promise<void> {
+ const environment = await db
+ .selectFrom('sandbox_environment')
+ .select('id')
+ .where('tenant_id', '=', input.tenant_id)
+ .where('name', '=', input.name)
+ .where('lifecycle_stage', '=', 'active')
+ .where(sql`json_extract(created_by_subject, '$.subject_id')`, '=', input.created_by_subject_id)
+ .executeTakeFirst();
+ if (environment === undefined) {
+ return;
+ }
+ await db.deleteFrom('sandbox_environment_secret').where('environment_id', '=', environment.id).execute();
await db
.updateTable('sandbox_environment')
.set({
lifecycle_stage: 'deleted',
updated_at: nowIso(),
})
- .where('tenant_id', '=', input.tenant_id)
- .where('name', '=', input.name)
- .where('lifecycle_stage', '=', 'active')
- .where(sql`json_extract(created_by_subject, '$.subject_id')`, '=', input.created_by_subject_id)
+ .where('id', '=', environment.id)
+ .execute();
+ }
+
+ async #buildVersion({
+ input,
+ environment_id,
+ db,
+ existing_version,
+ existing_manifest,
+ existing_external_ref,
+ }: {
+ input: UpsertSandboxEnvironmentInput;
+ environment_id: string;
+ db: Transaction<Database>;
+ existing_version?: number;
+ existing_manifest?: StoredSandboxEnvironmentManifest;
+ existing_external_ref?: string;
+ }): Promise<UpsertSandboxEnvironmentVersionWrite> {
+ const built = await input.buildVersion({
+ environment_id,
+ ...(existing_version ? { existing_version } : {}),
+ ...(existing_manifest ? { existing_manifest } : {}),
+ ...(existing_external_ref ? { existing_external_ref } : {}),
+ });
+ const secretDescription = `Secret value of environment ${built.manifest.name}`;
+ const existingSecretByName = new Map(
+ (await this.listSecretsByEnvironment({ environment_id }, db)).map(row => [row.secret_name, row]),
+ );
+ const syncedByName = new Map(input.synced_secrets.map(secret => [secret.secret_name, secret]));
+ const desiredNames = new Set((built.manifest.networking?.secrets ?? []).map(secret => secret.env));
+ const secrets: { key: string; id: string }[] = [];
+ for (const secret of built.manifest.networking?.secrets ?? []) {
+ const existing = existingSecretByName.get(secret.env);
+ const synced = syncedByName.get(secret.env);
+ if (synced === undefined) {
+ throw new Error(`Synced sandbox environment secret missing for ${secret.env}`);
+ }
+ const hash = !isRedactedSecretValue(secret.value)
+ ? hashSandboxEnvironmentSecret({ tenant_id: input.tenant_id, value: secret.value })
+ : existing?.hash;
+ if (!hash) {
+ throw new Error(`Sandbox environment secret hash missing for ${secret.env}`);
+ }
+ const timestamp = nowIso();
+ const row = await db
+ .insertInto('sandbox_environment_secret')
+ .values({
+ id: newId(),
+ tenant_id: input.tenant_id,
+ environment_id,
+ secret_name: secret.env,
+ external_secret_name: synced.external_secret_name,
+ external_secret_id: synced.external_secret_id,
+ description: secretDescription,
+ hash,
+ created_at: timestamp,
+ updated_at: timestamp,
+ })
+ .onConflict(oc =>
+ oc.columns(['environment_id', 'secret_name']).doUpdateSet({
+ external_secret_name: synced.external_secret_name,
+ external_secret_id: synced.external_secret_id,
+ description: secretDescription,
+ hash,
+ updated_at: timestamp,
+ }),
+ )
+ .returning('id')
+ .executeTakeFirstOrThrow();
+ secrets.push({ key: secret.env, id: row.id });
+ }
+ const removedIds = [...existingSecretByName.values()]
+ .filter(row => !desiredNames.has(row.secret_name))
+ .map(row => row.id);
+ if (removedIds.length > 0) {
+ await db.deleteFrom('sandbox_environment_secret').where('id', 'in', removedIds).execute();
+ }
+ const networking = built.manifest.networking;
+ return {
+ ...built,
+ manifest: {
+ ...built.manifest,
+ ...(networking?.secrets
+ ? {
+ networking: {
+ ...networking,
+ secrets: networking.secrets.map(secret => ({ ...secret, value: SECRET_REDACTION })),
+ },
+ }
+ : {}),
+ },
+ internal_metadata: { secrets },
+ };
+ }
+
+ async listSecretsByEnvironment(
+ input: { environment_id: string },
+ transaction?: Transaction<Database>,
+ ): Promise<SandboxEnvironmentSecretRecord[]> {
+ const db = transaction ?? this.#db;
+ const rows = await db
+ .selectFrom('sandbox_environment_secret')
+ .selectAll()
+ .where('environment_id', '=', input.environment_id)
.execute();
+ return rows.map(toSecretRecord);
+ }
+
+ async #withMountedSecrets(
+ loaded: SandboxEnvironmentWithVersion,
+ db: Kysely<Database> | Transaction<Database>,
+ ): Promise<SandboxEnvironmentWithVersion> {
+ const refs = loaded.version.internal_metadata.secrets;
+ if (refs.length === 0) {
+ return loaded;
+ }
+ const rows = await db
+ .selectFrom('sandbox_environment_secret')
+ .selectAll()
+ .where(
+ 'id',
+ 'in',
+ refs.map(ref => ref.id),
+ )
+ .execute();
+ const byId = new Map(rows.map(row => [row.id, row]));
+ const mounted_secrets: Record<string, string> = {};
+ for (const ref of refs) {
+ const row = byId.get(ref.id);
+ if (row !== undefined) {
+ mounted_secrets[ref.key] = row.external_secret_name;
+ }
+ }
+ return Object.keys(mounted_secrets).length > 0 ? { ...loaded, mounted_secrets } : loaded;
}
async #insertVersionRow(
diff --git a/packages/trueforge/src/db/sqlite/types.ts b/packages/trueforge/src/db/sqlite/types.ts
index cea7f3851..277bb85b4 100644
--- a/packages/trueforge/src/db/sqlite/types.ts
+++ b/packages/trueforge/src/db/sqlite/types.ts
@@ -297,6 +297,22 @@ export interface SandboxEnvironmentVersionTable {
updated_at: string;
}
+/**
+ * Per-environment Daytona org secret refs — mirrors Postgres (no plaintext value).
+ */
+export interface SandboxEnvironmentSecretTable {
+ id: string;
+ tenant_id: string;
+ environment_id: string;
+ secret_name: string;
+ external_secret_name: string;
+ external_secret_id: string;
+ description: string;
+ hash: string;
+ created_at: string;
+ updated_at: string;
+}
+
/**
* Configured schedules.
* PRIMARY KEY (id).
@@ -409,6 +425,7 @@ export interface Database {
agent: AgentTable;
sandbox_environment: SandboxEnvironmentTable;
sandbox_environment_version: SandboxEnvironmentVersionTable;
+ sandbox_environment_secret: SandboxEnvironmentSecretTable;
schedule: ScheduleTable;
schedule_run: ScheduleRunTable;
mcp_server: McpServerTable;
diff --git a/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts b/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts
index 197fa9e3d..53bfd00a6 100644
--- a/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts
+++ b/packages/trueforge/src/routes/sandboxEnvironmentRoutes.ts
@@ -123,7 +123,11 @@ export const putSandboxEnvironmentRoute = createRoute({
},
422: {
content: { 'application/json': { schema: RequestErrorResponseSchema } },
- description: 'No sandbox provider configured.',
+ description: 'Sandbox provider is missing or its credentials are invalid.',
+ },
+ 502: {
+ content: { 'application/json': { schema: RequestErrorResponseSchema } },
+ description: 'Sandbox provider secret synchronization failed.',
},
},
});
@@ -156,5 +160,13 @@ export const deleteSandboxEnvironmentRoute = createRoute({
content: { 'application/json': { schema: RequestErrorResponseSchema } },
description: 'Environment is referenced by one or more agents.',
},
+ 422: {
+ content: { 'application/json': { schema: RequestErrorResponseSchema } },
+ description: 'Sandbox provider is missing or its credentials are invalid.',
+ },
+ 502: {
+ content: { 'application/json': { schema: RequestErrorResponseSchema } },
+ description: 'Sandbox provider secret deletion failed.',
+ },
},
});
diff --git a/packages/trueforge/src/runtime/sessionResources.ts b/packages/trueforge/src/runtime/sessionResources.ts
index 9a8244077..9edd2281c 100644
--- a/packages/trueforge/src/runtime/sessionResources.ts
+++ b/packages/trueforge/src/runtime/sessionResources.ts
@@ -202,7 +202,7 @@ export async function resolveSandboxEnvironment({
sandboxEnvironmentStore: ISandboxEnvironmentStore;
optional?: boolean;
}): Promise<DaytonaSandboxEnvironment | undefined> {
- const loaded = await sandboxEnvironmentStore.getEnvironment({
+ const loaded = await sandboxEnvironmentStore.getActiveEnvironment({
tenant_id,
name,
});
@@ -222,9 +222,11 @@ export async function resolveSandboxEnvironment({
: `Sandbox environment "${name}" is not ready (status: ${loaded.version.status}) — retry shortly`,
});
}
+
return toSandboxEnvironment({
external_ref: loaded.version.external_ref,
manifest: loaded.version.manifest,
+ ...(loaded.mounted_secrets ? { mounted_secrets: loaded.mounted_secrets } : {}),
});
}
diff --git a/packages/trueforge/src/sandbox/ensureDefaultSandboxEnvironment.ts b/packages/trueforge/src/sandbox/ensureDefaultSandboxEnvironment.ts
index 7c3232b65..81459b406 100644
--- a/packages/trueforge/src/sandbox/ensureDefaultSandboxEnvironment.ts
+++ b/packages/trueforge/src/sandbox/ensureDefaultSandboxEnvironment.ts
@@ -4,18 +4,14 @@
import type { CreatedBySubject } from '@truefoundry/trueforge-core/agent-session';
import type { ISandboxEnvironmentStore, SandboxEnvironmentWithVersion } from '../db/sandboxEnvironmentStore';
import { NameSchema } from '../schemas/common';
+import { DEFAULT_SANDBOX_ENVIRONMENT_NAME } from '../schemas/sandboxEnvironment';
import {
- DEFAULT_SANDBOX_ENVIRONMENT_NAME,
- SandboxEnvironmentVersionInternalMetadataSchema,
-} from '../schemas/sandboxEnvironment';
-import {
+ buildNextVersion,
defaultSandboxEnvironmentStoredManifest,
- newExternalRef,
type SandboxEnvironmentProviderType,
} from './sandboxEnvironmentVersion';
const DEFAULT_NAME = NameSchema.parse(DEFAULT_SANDBOX_ENVIRONMENT_NAME);
-const EMPTY_INTERNAL_METADATA = SandboxEnvironmentVersionInternalMetadataSchema.parse({});
/**
* Returns the existing default env, or creates it / appends a new pending version.
@@ -46,15 +42,18 @@ export async function ensureDefaultSandboxEnvironment<TTransaction>({
name: DEFAULT_NAME,
description: '',
created_by_subject,
- buildVersion: previous => ({
- version: previous ? previous.latest_version + 1 : 1,
- manifest: defaultSandboxEnvironmentStoredManifest(provider_type),
- status: 'pending',
- status_reason: null,
- external_ref: newExternalRef(),
- internal_metadata: EMPTY_INTERNAL_METADATA,
- created_by_subject,
- }),
+ synced_secrets: [],
+ buildVersion: ({ existing_version, existing_manifest, existing_external_ref }) =>
+ Promise.resolve({
+ ...buildNextVersion({
+ ...(existing_version !== undefined ? { existing_version } : {}),
+ ...(existing_manifest ? { previous_manifest: existing_manifest } : {}),
+ ...(existing_external_ref ? { previous_external_ref: existing_external_ref } : {}),
+ manifest: defaultSandboxEnvironmentStoredManifest(provider_type),
+ provider_type,
+ }),
+ created_by_subject,
+ }),
},
transaction,
);
diff --git a/packages/trueforge/src/sandbox/local/provider/LocalSandboxProvider.ts b/packages/trueforge/src/sandbox/local/provider/LocalSandboxProvider.ts
index e04c3ed85..553253209 100644
--- a/packages/trueforge/src/sandbox/local/provider/LocalSandboxProvider.ts
+++ b/packages/trueforge/src/sandbox/local/provider/LocalSandboxProvider.ts
@@ -235,6 +235,18 @@ export class LocalSandboxProvider implements SandboxProvider {
return Promise.resolve();
}
+ createSecret(): Promise<{ id: string; name: string }> {
+ return Promise.reject(new Error('Local sandbox provider does not support org secrets'));
+ }
+
+ updateSecret(): Promise<void> {
+ return Promise.reject(new Error('Local sandbox provider does not support org secrets'));
+ }
+
+ deleteSecret(): Promise<void> {
+ return Promise.reject(new Error('Local sandbox provider does not support org secrets'));
+ }
+
/**
* Probe whether this host can run LocalSandboxProvider
* (OS + Code Mode UDS listen + SRT host binaries + in-sandbox shell + Python 3).
diff --git a/packages/trueforge/src/sandbox/progressSandboxEnvironmentVersion.ts b/packages/trueforge/src/sandbox/progressSandboxEnvironmentVersion.ts
index 732f264f8..6a803aa4d 100644
--- a/packages/trueforge/src/sandbox/progressSandboxEnvironmentVersion.ts
+++ b/packages/trueforge/src/sandbox/progressSandboxEnvironmentVersion.ts
@@ -1,23 +1,18 @@
-/**
- * Progress a pending sandbox-environment version: get/create snapshot, update DB.
- */
+/** Progress pending env version: snapshot build only (secrets sync on PUT). */
import { DAYTONA_SNAPSHOT_NOT_STARTED_REASON, withTimeout } from '@truefoundry/trueforge-core/core';
import { HTTPException } from 'hono/http-exception';
import type { Logger } from 'winston';
import type { ISandboxEnvironmentStore } from '../db/sandboxEnvironmentStore';
import type { ISandboxProviderStore } from '../db/sandboxProviderStore';
import {
- isDaytonaAuthError,
- isDaytonaPermissionError,
+ DAYTONA_RPC_TIMEOUT_MS,
+ getDaytonaAuthorizationErrorMessage,
toDaytonaSandboxProvider,
toSandboxEnvironment,
toSandboxStatus,
} from '../sandbox/providerUtils';
import { captureCriticalException } from '../sentry';
-/** Bound hung Daytona GET/POST so the sandbox-env-build tick can move on. */
-const DAYTONA_SNAPSHOT_RPC_TIMEOUT_MS = 30_000;
-
export async function progressSandboxEnvironmentVersion({
sandboxEnvironmentStore,
sandboxProviderStore,
@@ -57,32 +52,26 @@ export async function progressSandboxEnvironmentVersion({
try {
const status = await withTimeout(
provider.getBuildStatus(environment),
- DAYTONA_SNAPSHOT_RPC_TIMEOUT_MS,
+ DAYTONA_RPC_TIMEOUT_MS,
'sandbox environment getBuildStatus',
);
const built = toSandboxStatus(
status.reason === DAYTONA_SNAPSHOT_NOT_STARTED_REASON
- ? await withTimeout(provider.build(environment), DAYTONA_SNAPSHOT_RPC_TIMEOUT_MS, 'sandbox environment build')
+ ? await withTimeout(provider.build(environment), DAYTONA_RPC_TIMEOUT_MS, 'sandbox environment build')
: status,
);
if (built.status === 'ready') {
await sandboxEnvironmentStore.markVersionReady({ environment_version_id });
- return;
- }
- if (built.status === 'failed') {
+ } else if (built.status === 'failed') {
await sandboxEnvironmentStore.markVersionFailed({
environment_version_id,
status_reason: built.status_reason ?? 'Sandbox environment snapshot build failed',
});
- return;
}
// pending / building — leave as pending for the next tick.
- return;
} catch (error) {
- if (isDaytonaAuthError(error) || isDaytonaPermissionError(error)) {
- const status_reason = isDaytonaAuthError(error)
- ? 'Sandbox provider rejected the API key — check the credentials'
- : 'Sandbox provider denied access: the API key is missing required permissions';
+ const status_reason = getDaytonaAuthorizationErrorMessage(error);
+ if (status_reason !== undefined) {
await sandboxEnvironmentStore.markVersionFailed({ environment_version_id, status_reason });
logger.warn('Sandbox environment build failed authz', {
environment_version_id,
diff --git a/packages/trueforge/src/sandbox/providerUtils.ts b/packages/trueforge/src/sandbox/providerUtils.ts
index bb1bfb3c8..88b3f8d13 100644
--- a/packages/trueforge/src/sandbox/providerUtils.ts
+++ b/packages/trueforge/src/sandbox/providerUtils.ts
@@ -17,6 +17,9 @@ import {
type StoredSandboxProviderManifest,
} from '../schemas/sandboxProvider';
+/** Bound Daytona RPCs so API requests and controller ticks cannot hang indefinitely. */
+export const DAYTONA_RPC_TIMEOUT_MS = 5_000;
+
/** Provider rejected the credentials (401 unauthorized); retrying the same key cannot succeed. */
export function isDaytonaAuthError(error: unknown): boolean {
return error instanceof DaytonaError && error.statusCode === 401;
@@ -26,6 +29,20 @@ export function isDaytonaPermissionError(error: unknown): boolean {
return error instanceof DaytonaError && error.statusCode === 403;
}
+export function isDaytonaNotFoundError(error: unknown): boolean {
+ return error instanceof DaytonaError && error.statusCode === 404;
+}
+
+export function getDaytonaAuthorizationErrorMessage(error: unknown): string | undefined {
+ if (isDaytonaAuthError(error)) {
+ return 'Sandbox provider rejected the API key — check the credentials';
+ }
+ if (isDaytonaPermissionError(error)) {
+ return 'Sandbox provider denied access: the API key is missing required permissions';
+ }
+ return undefined;
+}
+
/** Configured tenant sandbox backends (not local fallback). */
export type ResolvedSandboxProvider = DaytonaSandboxProvider | TFYSandboxProvider;
@@ -33,15 +50,19 @@ export type ResolvedSandboxProvider = DaytonaSandboxProvider | TFYSandboxProvide
export function toSandboxEnvironment({
external_ref,
manifest,
+ mounted_secrets,
}: {
external_ref: string;
manifest: StoredSandboxEnvironmentManifest;
+ /** env var → Daytona org secret name for create mounts. */
+ mounted_secrets?: Record<string, string>;
}): DaytonaSandboxEnvironment {
return createDaytonaSandboxEnvironment({
snapshot_ref: external_ref,
resources: manifest.resources,
...(manifest.image ? { image: manifest.image } : {}),
...(manifest.environment_variables ? { environment_variables: manifest.environment_variables } : {}),
+ ...(mounted_secrets ? { mounted_secrets } : {}),
...(manifest.networking
? {
networking: {
diff --git a/packages/trueforge/src/sandbox/sandboxEnvironmentVersion.ts b/packages/trueforge/src/sandbox/sandboxEnvironmentVersion.ts
index ace7b4ea0..0bef85a55 100644
--- a/packages/trueforge/src/sandbox/sandboxEnvironmentVersion.ts
+++ b/packages/trueforge/src/sandbox/sandboxEnvironmentVersion.ts
@@ -7,22 +7,18 @@ import { isDeepStrictEqual } from 'node:util';
import {
DEFAULT_SANDBOX_ENVIRONMENT_NAME,
DEFAULT_SANDBOX_ENVIRONMENT_RESOURCES,
- SandboxEnvironmentVersionInternalMetadataSchema,
StoredSandboxEnvironmentManifestSchema,
type SandboxEnvironmentManifest,
- type SandboxEnvironmentVersionInternalMetadata,
type SandboxEnvironmentVersionStatus,
type StoredSandboxEnvironmentManifest,
} from '../schemas/sandboxEnvironment';
-import { isRedactedSecretValue, resolveStoredSecretValue, toRedactedSecretValue } from '../utils/secretRedaction';
+import { resolveStoredSecretValue, toRedactedSecretValue } from '../utils/secretRedaction';
export type SandboxEnvironmentProviderType = StoredSandboxEnvironmentManifest['type'];
export interface ManifestDiff {
build_changed: boolean;
resources_changed: boolean;
- /** Request includes at least one non-redacted secret value (new material to sync). */
- secrets_changed: boolean;
}
/** Next version row fields (status/external_ref encode whether a future controller must build). */
@@ -32,7 +28,6 @@ export interface NextSandboxEnvironmentVersion {
status: SandboxEnvironmentVersionStatus;
status_reason: null;
external_ref: string;
- internal_metadata: SandboxEnvironmentVersionInternalMetadata;
}
/** System default env stored jsonb (platform image; no networking/secrets). */
@@ -118,7 +113,6 @@ export function diffManifest({
return {
build_changed: previous?.image?.build_script !== next.image?.build_script,
resources_changed: previous === undefined || !isDeepStrictEqual(previous.resources, next.resources),
- secrets_changed: (next.networking?.secrets ?? []).some(secret => !isRedactedSecretValue(secret.value)),
};
}
@@ -127,15 +121,15 @@ export function newExternalRef(): string {
return `trueforge-${randomUUID()}`;
}
-/** Build the next version row fields (no insert). Secrets / Daytona sync intentionally skipped. */
+/** Next version row fields (no insert). */
export function buildNextVersion({
- version,
+ existing_version,
previous_manifest,
previous_external_ref,
manifest,
provider_type,
}: {
- version: number;
+ existing_version?: number;
previous_manifest?: StoredSandboxEnvironmentManifest;
previous_external_ref?: string;
manifest: SandboxEnvironmentManifest;
@@ -153,11 +147,10 @@ export function buildNextVersion({
// Always `pending` until a future controller activates (or fails) the version.
return {
- version,
+ version: (existing_version ?? 0) + 1,
manifest: toStoredManifest({ manifest: resolved, provider_type }),
status: 'pending',
status_reason: null,
external_ref: needs_snapshot ? newExternalRef() : previous_external_ref,
- internal_metadata: SandboxEnvironmentVersionInternalMetadataSchema.parse({}),
};
}
diff --git a/packages/trueforge/src/sandbox/syncSandboxEnvironmentSecrets.ts b/packages/trueforge/src/sandbox/syncSandboxEnvironmentSecrets.ts
new file mode 100644
index 000000000..1cbcf20c7
--- /dev/null
+++ b/packages/trueforge/src/sandbox/syncSandboxEnvironmentSecrets.ts
@@ -0,0 +1,125 @@
+import type { SandboxProvider } from '@truefoundry/trueforge-core/core';
+import { withTimeout } from '@truefoundry/trueforge-core/core';
+import { randomUUID } from 'node:crypto';
+import { isDeepStrictEqual } from 'node:util';
+import type { SandboxEnvironmentSecretRecord, SyncedSandboxEnvironmentSecret } from '../db/sandboxEnvironmentStore';
+import type { SandboxEnvironmentSecret } from '../schemas/sandboxEnvironment';
+import { isRedactedSecretValue, MissingStoredSecretError } from '../utils/secretRedaction';
+import { DAYTONA_RPC_TIMEOUT_MS, isDaytonaNotFoundError } from './providerUtils';
+
+export class SandboxEnvironmentSecretSyncError extends Error {
+ constructor(message: string, options: ErrorOptions) {
+ super(message, options);
+ this.name = 'SandboxEnvironmentSecretSyncError';
+ }
+}
+
+async function withSecretSyncError<T>(operation: Promise<T>): Promise<T> {
+ try {
+ return await operation;
+ } catch (error) {
+ throw new SandboxEnvironmentSecretSyncError(
+ error instanceof Error ? error.message : 'Sandbox environment secret sync failed',
+ { cause: error },
+ );
+ }
+}
+
+export async function deleteSandboxEnvironmentSecrets({
+ secrets,
+ provider,
+}: {
+ secrets: SandboxEnvironmentSecretRecord[];
+ provider: SandboxProvider<unknown>;
+}): Promise<void> {
+ for (const secret of secrets) {
+ try {
+ await withSecretSyncError(
+ withTimeout(
+ provider.deleteSecret({ secretId: secret.external_secret_id }),
+ DAYTONA_RPC_TIMEOUT_MS,
+ 'sandbox environment secret delete',
+ ),
+ );
+ } catch (error) {
+ if (error instanceof SandboxEnvironmentSecretSyncError && isDaytonaNotFoundError(error.cause)) {
+ continue;
+ }
+ throw error;
+ }
+ }
+}
+
+/** Sync provider org secrets from in-memory PUT values before the environment upsert. */
+export async function syncSandboxEnvironmentSecrets({
+ secrets,
+ previous,
+ existing,
+ provider,
+ description,
+}: {
+ secrets: SandboxEnvironmentSecret[];
+ previous: SandboxEnvironmentSecret[];
+ existing: SandboxEnvironmentSecretRecord[];
+ provider: SandboxProvider<unknown>;
+ description: string;
+}): Promise<SyncedSandboxEnvironmentSecret[]> {
+ const desiredNames = new Set(secrets.map(secret => secret.env));
+ const previousByName = new Map(previous.map(secret => [secret.env, secret]));
+ const byName = new Map(existing.map(row => [row.secret_name, row]));
+ const synced: SyncedSandboxEnvironmentSecret[] = [];
+
+ for (const secret of secrets) {
+ const row = byName.get(secret.env);
+ if (row !== undefined) {
+ const valueChanged = !isRedactedSecretValue(secret.value);
+ const hostsChanged = !isDeepStrictEqual(previousByName.get(secret.env)?.hosts, secret.hosts);
+ if (valueChanged || hostsChanged) {
+ await withSecretSyncError(
+ withTimeout(
+ provider.updateSecret({
+ secretId: row.external_secret_id,
+ hosts: secret.hosts,
+ ...(valueChanged ? { value: secret.value } : {}),
+ }),
+ DAYTONA_RPC_TIMEOUT_MS,
+ 'sandbox environment secret update',
+ ),
+ );
+ }
+ synced.push({
+ secret_name: secret.env,
+ external_secret_name: row.external_secret_name,
+ external_secret_id: row.external_secret_id,
+ });
+ continue;
+ }
+
+ if (isRedactedSecretValue(secret.value)) {
+ throw new MissingStoredSecretError();
+ }
+ const name = `trueforge-${randomUUID()}`;
+ const created = await withSecretSyncError(
+ withTimeout(
+ provider.createSecret({
+ name,
+ value: secret.value,
+ description,
+ hosts: secret.hosts,
+ }),
+ DAYTONA_RPC_TIMEOUT_MS,
+ 'sandbox environment secret create',
+ ),
+ );
+ synced.push({
+ secret_name: secret.env,
+ external_secret_name: name,
+ external_secret_id: created.id,
+ });
+ }
+
+ const removed = existing.filter(row => !desiredNames.has(row.secret_name));
+ await deleteSandboxEnvironmentSecrets({ secrets: removed, provider });
+
+ return synced;
+}
diff --git a/packages/trueforge/src/schemas/sandboxEnvironment.ts b/packages/trueforge/src/schemas/sandboxEnvironment.ts
index 55f8ef9b4..333cfad4e 100644
--- a/packages/trueforge/src/schemas/sandboxEnvironment.ts
+++ b/packages/trueforge/src/schemas/sandboxEnvironment.ts
@@ -191,6 +191,7 @@ export const DeleteSandboxEnvironmentResponseSchema = z.object({}).openapi('Dele
export type SandboxEnvironmentLifecycleStage = z.infer<typeof SandboxEnvironmentLifecycleStageSchema>;
export type SandboxEnvironmentVersionStatus = z.infer<typeof SandboxEnvironmentVersionStatusSchema>;
+export type SandboxEnvironmentSecret = z.infer<typeof SandboxEnvironmentSecretSchema>;
export type SandboxEnvironmentManifest = z.infer<typeof SandboxEnvironmentManifestSchema>;
export type StoredSandboxEnvironmentManifest = z.infer<typeof StoredSandboxEnvironmentManifestSchema>;
export type SandboxEnvironmentVersionInternalMetadata = z.infer<typeof SandboxEnvironmentVersionInternalMetadataSchema>;
diff --git a/packages/trueforge/src/truefoundry/TrueFoundrySandboxEnvironmentStore.ts b/packages/trueforge/src/truefoundry/TrueFoundrySandboxEnvironmentStore.ts
index 2c93933f8..b99b2140c 100644
--- a/packages/trueforge/src/truefoundry/TrueFoundrySandboxEnvironmentStore.ts
+++ b/packages/trueforge/src/truefoundry/TrueFoundrySandboxEnvironmentStore.ts
@@ -16,6 +16,7 @@ import type {
ListSandboxEnvironmentsInput,
MarkSandboxEnvironmentVersionFailedInput,
MarkSandboxEnvironmentVersionReadyInput,
+ SandboxEnvironmentSecretRecord,
SandboxEnvironmentVersionForProgress,
SandboxEnvironmentVersionRecord,
SandboxEnvironmentWithVersion,
@@ -104,6 +105,23 @@ export class TrueFoundrySandboxEnvironmentStore<
return this.#persistence.getEnvironment(input, transaction);
}
+ getActiveEnvironment(
+ input: GetSandboxEnvironmentInput,
+ transaction?: TTransaction,
+ ): Promise<SandboxEnvironmentWithVersion | undefined> {
+ if (!this.#envSupported) {
+ logger.info('Skipping active sandbox environment get (provider does not support environments)', {
+ name: input.name,
+ tenant_id: input.tenant_id,
+ });
+ return Promise.resolve(undefined);
+ }
+ if (input.name === DEFAULT_SANDBOX_ENVIRONMENT_NAME) {
+ return Promise.resolve(synthesizeTrueFoundryDefaultSandboxEnvironment(input.tenant_id));
+ }
+ return this.#persistence.getActiveEnvironment(input, transaction);
+ }
+
upsertEnvironment(
input: UpsertSandboxEnvironmentInput,
transaction?: TTransaction,
@@ -181,4 +199,11 @@ export class TrueFoundrySandboxEnvironmentStore<
}
return this.#persistence.deleteEnvironment(input, transaction);
}
+
+ listSecretsByEnvironment(
+ input: { environment_id: string },
+ transaction?: TTransaction,
+ ): Promise<SandboxEnvironmentSecretRecord[]> {
+ return this.#persistence.listSecretsByEnvironment(input, transaction);
+ }
}
diff --git a/packages/trueforge/src/utils/secretRedaction.ts b/packages/trueforge/src/utils/secretRedaction.ts
index 348ad0609..17a53aae7 100644
--- a/packages/trueforge/src/utils/secretRedaction.ts
+++ b/packages/trueforge/src/utils/secretRedaction.ts
@@ -1,3 +1,5 @@
+import { createHash } from 'node:crypto';
+
/**
* Redact secret strings for settings API responses and detect/merge
* redacted writebacks so clients can keep stored secrets without resending them.
@@ -8,6 +10,9 @@ const MIN_LENGTH_FOR_PREFIX_SUFFIX = 10;
/** Response mask: prefix/suffix for longer secrets; full mask when too short to hide. */
export function toRedactedSecretValue(secret: string): string {
+ if (isRedactedSecretValue(secret)) {
+ return secret;
+ }
if (secret.length < MIN_LENGTH_FOR_PREFIX_SUFFIX) {
return SECRET_REDACTION;
}
@@ -21,6 +26,11 @@ export function isRedactedSecretValue(value: string): boolean {
return value.includes(SECRET_REDACTION);
}
+/** sha256(tenant_id + "\\0" + value); reserved for later compare. */
+export function hashSandboxEnvironmentSecret({ tenant_id, value }: { tenant_id: string; value: string }): string {
+ return createHash('sha256').update(`${tenant_id}\0${value}`, 'utf8').digest('hex');
+}
+
/**
* Thrown when a redacted keep is requested but no stored secret exists
* (create with redacted stand-in, or keep for a missing field/header).
diff --git a/packages/trueforge/tests/unit/apis/capabilities.test.ts b/packages/trueforge/tests/unit/apis/capabilities.test.ts
index 3b1c37906..b095194d2 100644
--- a/packages/trueforge/tests/unit/apis/capabilities.test.ts
+++ b/packages/trueforge/tests/unit/apis/capabilities.test.ts
@@ -18,16 +18,15 @@ import type { SandboxEnvironmentVersionStatus } from '../../../src/schemas/sandb
const silentLogger = createLogger({ silent: true });
-let mockDefaultStatus: SandboxEnvironmentVersionStatus | undefined;
+let mockActiveDefaultStatus: SandboxEnvironmentVersionStatus | undefined;
+let mockLatestDefaultStatus: SandboxEnvironmentVersionStatus | undefined;
function mockDefaultEnvStore(): ISandboxEnvironmentStore {
+ const withStatus = (status: SandboxEnvironmentVersionStatus | undefined) =>
+ status === undefined ? undefined : ({ version: { status } } as unknown as SandboxEnvironmentWithVersion);
return {
- getEnvironment: () =>
- Promise.resolve(
- mockDefaultStatus === undefined
- ? undefined
- : ({ version: { status: mockDefaultStatus } } as unknown as SandboxEnvironmentWithVersion),
- ),
+ getEnvironment: () => Promise.resolve(withStatus(mockLatestDefaultStatus)),
+ getActiveEnvironment: () => Promise.resolve(withStatus(mockActiveDefaultStatus)),
} as unknown as ISandboxEnvironmentStore;
}
@@ -62,7 +61,8 @@ function withAuth(router: OpenAPIHono, authenticator: Authenticator): OpenAPIHon
describe('capabilities routers', () => {
beforeEach(() => {
- mockDefaultStatus = undefined;
+ mockActiveDefaultStatus = undefined;
+ mockLatestDefaultStatus = undefined;
setCachedLocalSandboxSupport(undefined);
});
@@ -100,7 +100,7 @@ describe('capabilities routers', () => {
it('reports sandbox + skill disabled when no image status is available', async () => {
disableOidcAuth();
- mockDefaultStatus = undefined;
+ mockActiveDefaultStatus = undefined;
const router = makeRouter();
const response = await router.request('/');
@@ -120,7 +120,7 @@ describe('capabilities routers', () => {
it('reports web_search enabled when a provider is configured', async () => {
disableOidcAuth();
- mockDefaultStatus = undefined;
+ mockActiveDefaultStatus = undefined;
const configuredStore: IWebSearchProviderStore = {
getProvider: () =>
Promise.resolve({
@@ -142,7 +142,7 @@ describe('capabilities routers', () => {
it('reports sandbox + skill enabled when local fallback is cached and no image status exists', async () => {
disableOidcAuth();
- mockDefaultStatus = undefined;
+ mockActiveDefaultStatus = undefined;
setCachedLocalSandboxSupport({
supported: true,
platform: 'darwin',
@@ -165,7 +165,7 @@ describe('capabilities routers', () => {
it('reports sandbox + skill enabled only when the image is ready', async () => {
disableOidcAuth();
- mockDefaultStatus = 'ready';
+ mockActiveDefaultStatus = 'ready';
const router = makeRouter();
const response = await router.request('/');
@@ -180,9 +180,26 @@ describe('capabilities routers', () => {
});
});
+ it('stays enabled while a newer default environment version is pending', async () => {
+ disableOidcAuth();
+ mockActiveDefaultStatus = 'ready';
+ mockLatestDefaultStatus = 'pending';
+ const router = makeRouter();
+
+ const response = await router.request('/');
+
+ expect(response.status).toBe(200);
+ expect(await response.json()).toMatchObject({
+ data: {
+ sandbox: { enabled: true },
+ skill: { enabled: true },
+ },
+ });
+ });
+
it('reports sandbox disabled with a "being prepared" skill reason while the image is still pending', async () => {
disableOidcAuth();
- mockDefaultStatus = 'pending';
+ mockActiveDefaultStatus = 'pending';
const router = makeRouter();
const response = await router.request('/');
@@ -200,7 +217,7 @@ describe('capabilities routers', () => {
it('reports "not configured" skill reason when the image build failed', async () => {
disableOidcAuth();
- mockDefaultStatus = 'failed';
+ mockActiveDefaultStatus = 'failed';
const router = makeRouter();
const response = await router.request('/');
diff --git a/packages/trueforge/tests/unit/apis/sandboxEnvironments.test.ts b/packages/trueforge/tests/unit/apis/sandboxEnvironments.test.ts
index 62218b339..c7ac12c8e 100644
--- a/packages/trueforge/tests/unit/apis/sandboxEnvironments.test.ts
+++ b/packages/trueforge/tests/unit/apis/sandboxEnvironments.test.ts
@@ -53,6 +53,7 @@ describe('sandbox environments API → build controller path', () => {
}) as never,
resolveSandboxProviderStore: () => sandboxProviderStore,
resolveRequestContext: () => STANDALONE_REQUEST_CONTEXT,
+ logger,
});
const buildRouter = createSandboxEnvironmentBuildRouter({
sandboxEnvironmentStore,
@@ -65,6 +66,22 @@ describe('sandbox environments API → build controller path', () => {
it('PUT custom env lands pending; internal progress marks ready after build', async () => {
const { publicRouter, buildRouter, sandboxEnvironmentStore } = await setup();
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret: jest.fn(),
+ updateSecret: jest.fn(),
+ deleteSecret: jest.fn(),
+ getBuildStatus: jest.fn().mockResolvedValue({
+ status: 'pending',
+ reason: DAYTONA_SNAPSHOT_NOT_STARTED_REASON,
+ metadata: null,
+ }),
+ build: jest.fn().mockResolvedValue({
+ status: 'ready',
+ reason: null,
+ metadata: null,
+ }),
+ } as never);
+
const putRes = await publicRouter.request('/', {
method: 'PUT',
headers: { 'content-type': 'application/json' },
@@ -87,19 +104,6 @@ describe('sandbox environments API → build controller path', () => {
const versionId = pendingBody.data[0]?.environment_version_id;
expect(versionId).toBeDefined();
- jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
- getBuildStatus: jest.fn().mockResolvedValue({
- status: 'pending',
- reason: DAYTONA_SNAPSHOT_NOT_STARTED_REASON,
- metadata: null,
- }),
- build: jest.fn().mockResolvedValue({
- status: 'ready',
- reason: null,
- metadata: null,
- }),
- } as never);
-
const progressRes = await buildRouter.request('/progress', {
method: 'POST',
headers: { 'content-type': 'application/json' },
@@ -114,6 +118,39 @@ describe('sandbox environments API → build controller path', () => {
const pendingAfter = await sandboxEnvironmentStore.listLatestPendingVersions();
expect(pendingAfter).toEqual([]);
+
+ const updateRes = await publicRouter.request('/', {
+ method: 'PUT',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({
+ manifest: {
+ name: 'pyjokes-env',
+ description: 'has requests',
+ image: { type: 'build', build_script: 'pip install requests' },
+ },
+ }),
+ });
+ expect(updateRes.status).toBe(200);
+
+ const latestGetRes = await publicRouter.request('/pyjokes-env');
+ const latestGetBody = (await latestGetRes.json()) as {
+ data: { status: string; manifest: { image?: { build_script?: string } } };
+ };
+ expect(latestGetBody.data.status).toBe('pending');
+ expect(latestGetBody.data.manifest.image?.build_script).toBe('pip install requests');
+
+ const latestListBody = (await (await publicRouter.request('/')).json()) as {
+ data: { status: string }[];
+ };
+ expect(latestListBody.data[0]?.status).toBe('pending');
+
+ const active = await sandboxEnvironmentStore.getActiveEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'pyjokes-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ });
+ expect(active?.version.status).toBe('ready');
+ expect(active?.version.manifest.image?.build_script).toBe('pip install pyjokes');
});
it('rejects creating the reserved default name', async () => {
diff --git a/packages/trueforge/tests/unit/sandbox/ensureDefaultSandboxEnvironment.test.ts b/packages/trueforge/tests/unit/sandbox/ensureDefaultSandboxEnvironment.test.ts
index 12d6d472c..d877edf17 100644
--- a/packages/trueforge/tests/unit/sandbox/ensureDefaultSandboxEnvironment.test.ts
+++ b/packages/trueforge/tests/unit/sandbox/ensureDefaultSandboxEnvironment.test.ts
@@ -59,11 +59,17 @@ describe('ensureDefaultSandboxEnvironment', () => {
}),
undefined,
);
- const buildVersion = store.upsertEnvironment.mock.calls[0]?.[0].buildVersion as (previous?: {
- latest_version: number;
- }) => { status: string; version: number };
- expect(buildVersion(undefined)).toMatchObject({ status: 'pending', version: 1 });
- expect(buildVersion({ latest_version: 3 })).toMatchObject({ status: 'pending', version: 4 });
+ const buildVersion = store.upsertEnvironment.mock.calls[0]?.[0].buildVersion as (input: {
+ existing_version?: number;
+ }) => Promise<{ status: string; version: number }>;
+ await expect(buildVersion({})).resolves.toMatchObject({
+ status: 'pending',
+ version: 1,
+ });
+ await expect(buildVersion({ existing_version: 3 })).resolves.toMatchObject({
+ status: 'pending',
+ version: 4,
+ });
});
it('upserts again when resetPending is true even if default exists', async () => {
diff --git a/packages/trueforge/tests/unit/sandbox/progressSandboxEnvironmentVersion.test.ts b/packages/trueforge/tests/unit/sandbox/progressSandboxEnvironmentVersion.test.ts
index a8712b058..5f54b4a97 100644
--- a/packages/trueforge/tests/unit/sandbox/progressSandboxEnvironmentVersion.test.ts
+++ b/packages/trueforge/tests/unit/sandbox/progressSandboxEnvironmentVersion.test.ts
@@ -15,10 +15,7 @@ jest.mock('../../../src/sandbox/providerUtils', () => {
const actual = jest.requireActual<typeof import('../../../src/sandbox/providerUtils')>(
'../../../src/sandbox/providerUtils',
);
- return {
- ...actual,
- toDaytonaSandboxProvider: jest.fn(),
- };
+ return { ...actual, toDaytonaSandboxProvider: jest.fn() };
});
const logger = createLogger({ silent: true });
@@ -37,6 +34,7 @@ const PENDING = {
sandbox_provider: 'daytona' as const,
image: { type: 'build' as const, build_script: 'pip install pyjokes' },
},
+ internal_metadata: { secrets: [] as { key: string; id: string }[] },
};
const PROVIDER_RECORD = {
@@ -60,18 +58,18 @@ function makeStores(options?: { pending?: typeof PENDING | null; provider?: type
getSandboxEnvironmentVersion: jest.fn().mockResolvedValue(pending),
markVersionReady: jest.fn().mockResolvedValue(undefined),
markVersionFailed: jest.fn().mockResolvedValue(undefined),
- };
- const sandboxProviderStore = {
- getSandboxProvider: jest.fn().mockResolvedValue(provider),
+ listSecretsByEnvironment: jest.fn().mockResolvedValue([]),
};
return {
sandboxEnvironmentStore: sandboxEnvironmentStore as unknown as ISandboxEnvironmentStore,
- sandboxProviderStore: sandboxProviderStore as unknown as ISandboxProviderStore,
+ sandboxProviderStore: {
+ getSandboxProvider: jest.fn().mockResolvedValue(provider),
+ } as unknown as ISandboxProviderStore,
envStore: sandboxEnvironmentStore,
};
}
-function mockProvider(methods: { getBuildStatus: jest.Mock; build: jest.Mock }): void {
+function mockProvider(methods: Record<string, jest.Mock>): void {
jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue(methods as never);
}
@@ -91,15 +89,13 @@ describe('progressSandboxEnvironmentVersion', () => {
logger,
}),
).rejects.toMatchObject({ status: 404 } satisfies Partial<HTTPException>);
- expect(sentry.captureCriticalException).toHaveBeenCalled();
});
it('marks ready when getBuildStatus reports ready', async () => {
const { sandboxEnvironmentStore, sandboxProviderStore, envStore } = makeStores();
- const build = jest.fn();
mockProvider({
getBuildStatus: jest.fn().mockResolvedValue({ status: 'ready', reason: null, metadata: null }),
- build,
+ build: jest.fn(),
});
await progressSandboxEnvironmentVersion({
@@ -109,8 +105,9 @@ describe('progressSandboxEnvironmentVersion', () => {
logger,
});
- expect(build).not.toHaveBeenCalled();
- expect(envStore.markVersionReady).toHaveBeenCalledWith({ environment_version_id: 'ver-1' });
+ expect(envStore.markVersionReady).toHaveBeenCalledWith({
+ environment_version_id: 'ver-1',
+ });
});
it('calls build when snapshot has not started, then marks ready', async () => {
@@ -133,7 +130,7 @@ describe('progressSandboxEnvironmentVersion', () => {
});
expect(build).toHaveBeenCalledTimes(1);
- expect(envStore.markVersionReady).toHaveBeenCalledWith({ environment_version_id: 'ver-1' });
+ expect(envStore.markVersionReady).toHaveBeenCalled();
});
it('marks failed when build status is failed', async () => {
@@ -159,50 +156,4 @@ describe('progressSandboxEnvironmentVersion', () => {
status_reason: 'pip blew up',
});
});
-
- it('leaves pending when still building', async () => {
- const { sandboxEnvironmentStore, sandboxProviderStore, envStore } = makeStores();
- mockProvider({
- getBuildStatus: jest.fn().mockResolvedValue({
- status: 'pending',
- reason: 'building…',
- metadata: null,
- }),
- build: jest.fn(),
- });
-
- await progressSandboxEnvironmentVersion({
- sandboxEnvironmentStore,
- sandboxProviderStore,
- environment_version_id: 'ver-1',
- logger,
- });
-
- expect(envStore.markVersionReady).not.toHaveBeenCalled();
- expect(envStore.markVersionFailed).not.toHaveBeenCalled();
- });
-
- it('times out a hung getBuildStatus so the build loop can continue', async () => {
- jest.useFakeTimers();
- try {
- const { sandboxEnvironmentStore, sandboxProviderStore } = makeStores();
- mockProvider({
- getBuildStatus: jest.fn().mockReturnValue(new Promise(() => undefined)),
- build: jest.fn(),
- });
-
- const progressPromise = progressSandboxEnvironmentVersion({
- sandboxEnvironmentStore,
- sandboxProviderStore,
- environment_version_id: 'ver-1',
- logger,
- });
- const expectation = expect(progressPromise).rejects.toThrow(/Timed out.*getBuildStatus/);
- await jest.advanceTimersByTimeAsync(30_000);
- await expectation;
- expect(sentry.captureCriticalException).toHaveBeenCalled();
- } finally {
- jest.useRealTimers();
- }
- });
});
diff --git a/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentSecrets.test.ts b/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentSecrets.test.ts
new file mode 100644
index 000000000..307b3f399
--- /dev/null
+++ b/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentSecrets.test.ts
@@ -0,0 +1,332 @@
+import { DaytonaError } from '@daytona/sdk';
+import { createLogger } from 'winston';
+import { createSandboxEnvironmentBuildRouter } from '../../../src/apis/sandboxEnvironmentBuild';
+import { createSandboxEnvironmentsRouter } from '../../../src/apis/sandboxEnvironments';
+import { STANDALONE_REQUEST_CONTEXT } from '../../../src/auth/identity';
+import { migrateSqliteToLatest } from '../../../src/db/migrateSqlite';
+import { createSqliteDb } from '../../../src/db/sqlite/client';
+import { SqliteSandboxEnvironmentStore } from '../../../src/db/sqlite/sandbox-environment-store/SqliteSandboxEnvironmentStore';
+import { SqliteSandboxProviderStore } from '../../../src/db/sqlite/sandbox-provider-store/SqliteSandboxProviderStore';
+import { resolveSandboxEnvironment } from '../../../src/runtime/sessionResources';
+import * as providerUtils from '../../../src/sandbox/providerUtils';
+import { SECRET_REDACTION } from '../../../src/utils/secretRedaction';
+
+jest.mock('../../../src/sandbox/providerUtils', () => {
+ const actual = jest.requireActual<typeof import('../../../src/sandbox/providerUtils')>(
+ '../../../src/sandbox/providerUtils',
+ );
+ return { ...actual, toDaytonaSandboxProvider: jest.fn() };
+});
+
+const logger = createLogger({ silent: true });
+
+const daytonaManifest = {
+ type: 'daytona' as const,
+ auth: { api_key: 'dtn-test-secret' },
+ exec_timeout_ms: 60_000,
+ auto_stop_interval_in_minutes: 5,
+ auto_archive_interval_in_minutes: 60,
+ auto_delete_interval_in_minutes: 7200,
+};
+
+describe('sandbox environment secrets', () => {
+ afterEach(() => {
+ jest.restoreAllMocks();
+ });
+
+ async function setup() {
+ const db = createSqliteDb(':memory:');
+ await migrateSqliteToLatest(db);
+ const sandboxEnvironmentStore = new SqliteSandboxEnvironmentStore(db);
+ const sandboxProviderStore = new SqliteSandboxProviderStore(db);
+ await sandboxProviderStore.upsertSandboxProvider({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ manifest: daytonaManifest,
+ });
+ const publicRouter = createSandboxEnvironmentsRouter({
+ sandboxEnvironmentStore,
+ resolveAgentStore: () => ({ listAgentNamesUsingSandboxEnvironment: jest.fn().mockResolvedValue([]) }) as never,
+ resolveSandboxProviderStore: () => sandboxProviderStore,
+ resolveRequestContext: () => STANDALONE_REQUEST_CONTEXT,
+ logger,
+ });
+ const buildRouter = createSandboxEnvironmentBuildRouter({
+ sandboxEnvironmentStore,
+ sandboxProviderStore,
+ logger,
+ });
+ const putEnvironment = (manifest: unknown) =>
+ publicRouter.request('/', {
+ method: 'PUT',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ manifest }),
+ });
+ return { publicRouter, buildRouter, sandboxEnvironmentStore, putEnvironment };
+ }
+
+ it('upserts secret rows and syncs to Daytona on PUT, redacts, and mounts names after ready', async () => {
+ const { buildRouter, sandboxEnvironmentStore, putEnvironment } = await setup();
+
+ const createSecret = jest
+ .fn()
+ .mockImplementation(({ name }: { name: string }) => Promise.resolve({ id: 'daytona-sec-1', name }));
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ getBuildStatus: jest.fn().mockResolvedValue({ status: 'ready', reason: null, metadata: null }),
+ build: jest.fn(),
+ createSecret,
+ updateSecret: jest.fn(),
+ deleteSecret: jest.fn(),
+ } as never);
+
+ const putRes = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+ expect(putRes.status).toBe(200);
+ const putBody = (await putRes.json()) as {
+ data: {
+ id: string;
+ status: string;
+ manifest: { networking?: { secrets?: { value: string }[] } };
+ };
+ };
+ expect(putBody.data.status).toBe('pending');
+ expect(putBody.data.manifest.networking?.secrets?.[0]?.value).toBe(SECRET_REDACTION);
+
+ const rows = await sandboxEnvironmentStore.listSecretsByEnvironment({
+ environment_id: putBody.data.id,
+ });
+ expect(rows).toEqual([
+ expect.objectContaining({
+ secret_name: 'GITHUB_TOKEN',
+ external_secret_id: 'daytona-sec-1',
+ external_secret_name: expect.stringMatching(/^trueforge-/),
+ }),
+ ]);
+ expect(createSecret).toHaveBeenCalledWith(
+ expect.objectContaining({
+ name: rows[0]?.external_secret_name,
+ value: 'ghp_plain',
+ hosts: ['github.com'],
+ }),
+ );
+
+ const pendingAfterPut = await sandboxEnvironmentStore.getEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ });
+ expect(pendingAfterPut?.version.manifest.networking?.secrets?.[0]?.value).toBe(SECRET_REDACTION);
+
+ const pending = (await (await buildRouter.request('/pending')).json()) as {
+ data: { environment_version_id: string }[];
+ };
+ const versionId = pending.data[0]?.environment_version_id;
+ expect(versionId).toBeDefined();
+
+ expect(
+ (
+ await buildRouter.request('/progress', {
+ method: 'POST',
+ headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ environment_version_id: versionId }),
+ })
+ ).status,
+ ).toBe(204);
+
+ expect(createSecret).toHaveBeenCalledTimes(1);
+
+ const loaded = await sandboxEnvironmentStore.getEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ });
+ expect(loaded?.version.status).toBe('ready');
+ expect(loaded?.version.manifest.networking?.secrets?.[0]?.value).toBe(SECRET_REDACTION);
+ expect(loaded?.mounted_secrets).toEqual({
+ GITHUB_TOKEN: rows[0]?.external_secret_name,
+ });
+
+ const environment = await resolveSandboxEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ sandboxEnvironmentStore,
+ });
+ expect(environment?.mounted_secrets).toEqual({
+ GITHUB_TOKEN: rows[0]?.external_secret_name,
+ });
+ });
+
+ it('updates a redacted secret and removes its Daytona and database refs on PUT', async () => {
+ const { sandboxEnvironmentStore, putEnvironment } = await setup();
+ const createSecret = jest
+ .fn()
+ .mockImplementation(({ name }: { name: string }) => Promise.resolve({ id: 'daytona-sec-1', name }));
+ const updateSecret = jest.fn().mockResolvedValue(undefined);
+ const deleteSecret = jest.fn().mockResolvedValue(undefined);
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret,
+ updateSecret,
+ deleteSecret,
+ } as never);
+
+ const createResponse = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+ expect(createResponse.status).toBe(200);
+ const created = (await createResponse.json()) as { data: { id: string } };
+ const originalRows = await sandboxEnvironmentStore.listSecretsByEnvironment({
+ environment_id: created.data.id,
+ });
+
+ const unchangedResponse = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: SECRET_REDACTION, hosts: ['github.com'] }],
+ },
+ });
+ expect(unchangedResponse.status).toBe(200);
+ expect(updateSecret).not.toHaveBeenCalled();
+
+ const updateResponse = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: SECRET_REDACTION, hosts: ['api.github.com'] }],
+ },
+ });
+ expect(updateResponse.status).toBe(200);
+ expect(updateSecret).toHaveBeenCalledWith({
+ secretId: 'daytona-sec-1',
+ hosts: ['api.github.com'],
+ });
+ expect(await sandboxEnvironmentStore.listSecretsByEnvironment({ environment_id: created.data.id })).toEqual([
+ expect.objectContaining({
+ id: originalRows[0]?.id,
+ hash: originalRows[0]?.hash,
+ external_secret_name: originalRows[0]?.external_secret_name,
+ external_secret_id: 'daytona-sec-1',
+ }),
+ ]);
+
+ const removeResponse = await putEnvironment({ name: 'secret-env' });
+ expect(removeResponse.status).toBe(200);
+ expect(deleteSecret).toHaveBeenCalledWith({ secretId: 'daytona-sec-1' });
+ expect(await sandboxEnvironmentStore.listSecretsByEnvironment({ environment_id: created.data.id })).toEqual([]);
+ });
+
+ it('deletes Daytona secrets and secret rows when deleting an environment', async () => {
+ const { publicRouter, sandboxEnvironmentStore, putEnvironment } = await setup();
+ const deleteSecret = jest.fn().mockResolvedValue(undefined);
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret: jest.fn().mockResolvedValue({ id: 'daytona-sec-1', name: 'trueforge-secret-1' }),
+ updateSecret: jest.fn(),
+ deleteSecret,
+ } as never);
+
+ const createResponse = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+ const created = (await createResponse.json()) as { data: { id: string } };
+
+ const deleteResponse = await publicRouter.request('/secret-env', { method: 'DELETE' });
+
+ expect(deleteResponse.status).toBe(200);
+ expect(deleteSecret).toHaveBeenCalledWith({ secretId: 'daytona-sec-1' });
+ expect(await sandboxEnvironmentStore.listSecretsByEnvironment({ environment_id: created.data.id })).toEqual([]);
+ expect(
+ await sandboxEnvironmentStore.getEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ }),
+ ).toBeUndefined();
+ });
+
+ it('keeps the environment when Daytona secret deletion fails', async () => {
+ const { publicRouter, sandboxEnvironmentStore, putEnvironment } = await setup();
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret: jest.fn().mockResolvedValue({ id: 'daytona-sec-1', name: 'trueforge-secret-1' }),
+ updateSecret: jest.fn(),
+ deleteSecret: jest.fn().mockRejectedValue(new Error('Daytona secret delete failed')),
+ } as never);
+
+ await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+
+ const deleteResponse = await publicRouter.request('/secret-env', { method: 'DELETE' });
+
+ expect(deleteResponse.status).toBe(502);
+ expect(await deleteResponse.json()).toEqual({
+ error: { message: 'Daytona secret delete failed' },
+ });
+ expect(
+ await sandboxEnvironmentStore.getEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ }),
+ ).toBeDefined();
+ });
+
+ it('returns 502 when Daytona secret sync fails on PUT', async () => {
+ const { sandboxEnvironmentStore, putEnvironment } = await setup();
+
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret: jest.fn().mockRejectedValue(new Error('Daytona secret create failed')),
+ updateSecret: jest.fn(),
+ deleteSecret: jest.fn(),
+ } as never);
+
+ const putRes = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+ expect(putRes.status).toBe(502);
+ expect(await putRes.json()).toEqual({
+ error: { message: 'Daytona secret create failed' },
+ });
+
+ const loaded = await sandboxEnvironmentStore.getEnvironment({
+ tenant_id: STANDALONE_REQUEST_CONTEXT.tenant_id,
+ name: 'secret-env',
+ created_by_subject_id: STANDALONE_REQUEST_CONTEXT.subject.id,
+ });
+ expect(loaded).toBeUndefined();
+ });
+
+ it('returns 422 when Daytona rejects the provider credentials', async () => {
+ const { putEnvironment } = await setup();
+
+ jest.mocked(providerUtils.toDaytonaSandboxProvider).mockReturnValue({
+ createSecret: jest.fn().mockRejectedValue(new DaytonaError('Unauthorized', 401)),
+ updateSecret: jest.fn(),
+ deleteSecret: jest.fn(),
+ } as never);
+
+ const putRes = await putEnvironment({
+ name: 'secret-env',
+ networking: {
+ secrets: [{ env: 'GITHUB_TOKEN', value: 'ghp_plain', hosts: ['github.com'] }],
+ },
+ });
+
+ expect(putRes.status).toBe(422);
+ expect(await putRes.json()).toEqual({
+ error: { message: 'Sandbox provider rejected the API key — check the credentials' },
+ });
+ });
+});
diff --git a/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentVersion.test.ts b/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentVersion.test.ts
index 9fb57f273..0913fc8aa 100644
--- a/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentVersion.test.ts
+++ b/packages/trueforge/tests/unit/sandbox/sandboxEnvironmentVersion.test.ts
@@ -13,7 +13,7 @@ describe('buildNextVersion', () => {
it('allocates a new external_ref when resources change without a build image', () => {
const next = buildNextVersion({
- version: 2,
+ existing_version: 1,
previous_manifest: previous,
previous_external_ref: 'trueforge-old',
manifest: {
@@ -24,13 +24,14 @@ describe('buildNextVersion', () => {
});
expect(next.external_ref).not.toBe('trueforge-old');
+ expect(next.version).toBe(2);
expect(next.status).toBe('pending');
expect(next.manifest.resources).toEqual({ cpu: 2, memory: 4, disk: 8 });
});
it('reuses previous_external_ref when only networking changes', () => {
const next = buildNextVersion({
- version: 2,
+ existing_version: 1,
previous_manifest: previous,
previous_external_ref: 'trueforge-old',
manifest: {
@@ -50,7 +51,7 @@ describe('buildNextVersion', () => {
image: { type: 'build' as const, build_script: 'pip install httpx' },
};
const next = buildNextVersion({
- version: 2,
+ existing_version: 1,
previous_manifest: withBuild,
previous_external_ref: 'trueforge-old',
manifest: {
diff --git a/packages/trueforge/tests/unit/truefoundry/TrueFoundrySandboxEnvironmentStore.test.ts b/packages/trueforge/tests/unit/truefoundry/TrueFoundrySandboxEnvironmentStore.test.ts
index 9bf56df05..3eb4fafcf 100644
--- a/packages/trueforge/tests/unit/truefoundry/TrueFoundrySandboxEnvironmentStore.test.ts
+++ b/packages/trueforge/tests/unit/truefoundry/TrueFoundrySandboxEnvironmentStore.test.ts
@@ -68,7 +68,8 @@ describe('TrueFoundrySandboxEnvironmentStore', () => {
subject_type: 'user',
subject_display_name: 'User',
},
- buildVersion: () => ({
+ synced_secrets: [],
+ buildVersion: async () => ({
version: 1,
manifest: {
name: 'pyjokes-env',
@@ -79,7 +80,6 @@ describe('TrueFoundrySandboxEnvironmentStore', () => {
status: 'pending',
status_reason: null,
external_ref: 'ref-1',
- internal_metadata: { secrets: [] },
created_by_subject: {
subject_id: SUBJECT_ID,
subject_type: 'user',
diff --git a/python/trueforge_sdk/src/trueforge_sdk/sandbox_environments/raw_client.py b/python/trueforge_sdk/src/trueforge_sdk/sandbox_environments/raw_client.py
index d344bd2f3..3473cc049 100644
--- a/python/trueforge_sdk/src/trueforge_sdk/sandbox_environments/raw_client.py
+++ b/python/trueforge_sdk/src/trueforge_sdk/sandbox_environments/raw_client.py
@@ -12,6 +12,7 @@ from ..core.parse_error import ParsingError
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..core.unchecked_base_model import construct_type
+from ..errors.bad_gateway_error import BadGatewayError
from ..errors.bad_request_error import BadRequestError
from ..errors.conflict_error import ConflictError
from ..errors.not_found_error import NotFoundError
@@ -206,6 +207,17 @@ class RawSandboxEnvironmentsClient:
),
),
)
+ if _response.status_code == 502:
+ raise BadGatewayError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
_response_json = _response.json()
except JSONDecodeError:
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
@@ -347,6 +359,28 @@ class RawSandboxEnvironmentsClient:
),
),
)
+ if _response.status_code == 422:
+ raise UnprocessableEntityError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
+ if _response.status_code == 502:
+ raise BadGatewayError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
_response_json = _response.json()
except JSONDecodeError:
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
@@ -537,6 +571,17 @@ class AsyncRawSandboxEnvironmentsClient:
),
),
)
+ if _response.status_code == 502:
+ raise BadGatewayError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
_response_json = _response.json()
except JSONDecodeError:
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
@@ -678,6 +723,28 @@ class AsyncRawSandboxEnvironmentsClient:
),
),
)
+ if _response.status_code == 422:
+ raise UnprocessableEntityError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
+ if _response.status_code == 502:
+ raise BadGatewayError(
+ headers=dict(_response.headers),
+ body=typing.cast(
+ RequestErrorResponse,
+ construct_type(
+ type_=RequestErrorResponse, # type: ignore
+ object_=_response.json(),
+ ),
+ ),
+ )
_response_json = _response.json()
except JSONDecodeError:
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)