chore: pr feedbacks

This commit is contained in:
Nicolas Meienberger 2026-05-07 20:36:57 +02:00
parent 854e2778f4
commit 3584ab9896
No known key found for this signature in database
3 changed files with 18 additions and 34 deletions

View file

@ -3,11 +3,7 @@ import { db } from "../../db/db";
import { logger } from "@zerobyte/core/node"; import { logger } from "@zerobyte/core/node";
import { stopApplicationRuntime } from "./bootstrap"; import { stopApplicationRuntime } from "./bootstrap";
import { decryptVolumeConfig } from "../volumes/volume-config-secrets"; import { decryptVolumeConfig } from "../volumes/volume-config-secrets";
import { import { createVolumeBackend } from "../../../../apps/agent/src/volume-host";
createVolumeBackend,
type AgentVolume,
type BackendConfig as HostBackendConfig,
} from "../../../../apps/agent/src/volume-host";
export const shutdown = async () => { export const shutdown = async () => {
await Scheduler.stop(); await Scheduler.stop();
@ -20,9 +16,9 @@ export const shutdown = async () => {
for (const volume of volumes) { for (const volume of volumes) {
const backend = createVolumeBackend({ const backend = createVolumeBackend({
...volume, ...volume,
config: (await decryptVolumeConfig(volume.config)) as HostBackendConfig, config: await decryptVolumeConfig(volume.config),
provisioningId: volume.provisioningId ?? null, provisioningId: volume.provisioningId ?? null,
} satisfies AgentVolume); });
const { status, error } = await backend.unmount(); const { status, error } = await backend.unmount();
logger.info(`Volume ${volume.name} unmount status: ${status}${error ? `, error: ${error}` : ""}`); logger.info(`Volume ${volume.name} unmount status: ${status}${error ? `, error: ${error}` : ""}`);

View file

@ -18,18 +18,13 @@ import { getOrganizationId } from "~/server/core/request-context";
import { type ShortId } from "~/server/utils/branded"; import { type ShortId } from "~/server/utils/branded";
import { decryptVolumeConfig, encryptVolumeConfig } from "./volume-config-secrets"; import { decryptVolumeConfig, encryptVolumeConfig } from "./volume-config-secrets";
import type { VolumeCommand, VolumeCommandResult } from "@zerobyte/contracts/agent-protocol"; import type { VolumeCommand, VolumeCommandResult } from "@zerobyte/contracts/agent-protocol";
import { import { createVolumeBackend, getStatFs, getVolumePath } from "../../../../apps/agent/src/volume-host";
createVolumeBackend,
getStatFs,
getVolumePath,
type AgentVolume,
type BackendConfig as HostBackendConfig,
} from "../../../../apps/agent/src/volume-host";
import { import {
browseFilesystem as browseHostFilesystem, browseFilesystem as browseHostFilesystem,
listVolumeFiles, listVolumeFiles,
testVolumeConnection, testVolumeConnection,
} from "../../../../apps/agent/src/volume-host/operations"; } from "../../../../apps/agent/src/volume-host/operations";
import { Effect } from "effect";
type EnsureHealthyVolumeResult = type EnsureHealthyVolumeResult =
| { ready: true; volume: Volume; remounted: boolean } | { ready: true; volume: Volume; remounted: boolean }
@ -68,10 +63,10 @@ const volumeForAgent = async (volume: Volume): Promise<Volume> => ({
config: await decryptVolumeConfig(volume.config), config: await decryptVolumeConfig(volume.config),
}); });
const volumeForHost = async (volume: Volume): Promise<AgentVolume> => ({ const volumeForHost = async (volume: Volume): Promise<Volume> => ({
...volume, ...volume,
shortId: volume.shortId, shortId: volume.shortId,
config: (await decryptVolumeConfig(volume.config)) as HostBackendConfig, config: await decryptVolumeConfig(volume.config),
provisioningId: volume.provisioningId ?? null, provisioningId: volume.provisioningId ?? null,
}); });
@ -101,17 +96,6 @@ const runVolumeBackendCommand = async (
return command.result; return command.result;
}; };
const mapAgentFileError = (error: unknown) => {
const message = toMessage(error);
if (message === "Invalid path") {
throw new BadRequestError("Invalid path");
}
if (message === "Directory not found") {
throw new NotFoundError("Directory not found");
}
throw error;
};
const createVolume = async (name: string, backendConfig: BackendConfig) => { const createVolume = async (name: string, backendConfig: BackendConfig) => {
const organizationId = getOrganizationId(); const organizationId = getOrganizationId();
const trimmedName = name.trim(); const trimmedName = name.trim();
@ -219,9 +203,10 @@ const getVolume = async (shortId: ShortId) => {
if (volume.status === "mounted") { if (volume.status === "mounted") {
statfs = await withTimeout( statfs = await withTimeout(
shouldRunViaAgent(volume) shouldRunViaAgent(volume)
? runVolumeCommand(volume.agentId, { name: "volume.statfs", volume: await volumeForAgent(volume) }).then( ? runVolumeCommand(volume.agentId, {
(command) => command.result, name: "volume.statfs",
) volume: await volumeForAgent(volume),
}).then((command) => command.result)
: volumeForHost(volume).then((hostVolume) => getStatFs(getVolumePath(hostVolume))), : volumeForHost(volume).then((hostVolume) => getStatFs(getVolumePath(hostVolume))),
1000, 1000,
"volume.statfs", "volume.statfs",
@ -295,7 +280,7 @@ const updateVolume = async (shortId: ShortId, volumeData: UpdateVolumeBody) => {
const testConnection = async (backendConfig: BackendConfig) => { const testConnection = async (backendConfig: BackendConfig) => {
if (!config.flags.enableLocalAgent) { if (!config.flags.enableLocalAgent) {
return testVolumeConnection(backendConfig as HostBackendConfig); return Effect.runPromise(testVolumeConnection(backendConfig));
} }
const command = await runVolumeCommand(LOCAL_AGENT_ID, { name: "volume.testConnection", backendConfig }); const command = await runVolumeCommand(LOCAL_AGENT_ID, { name: "volume.testConnection", backendConfig });
@ -404,7 +389,6 @@ const listFiles = async (shortId: ShortId, subPath?: string, offset: number = 0,
}); });
return command.result; return command.result;
} catch (error) { } catch (error) {
mapAgentFileError(error);
throw new InternalServerError(`Failed to list files: ${toMessage(error)}`); throw new InternalServerError(`Failed to list files: ${toMessage(error)}`);
} }
}; };

View file

@ -20,7 +20,11 @@ export const listVolumeFiles = async (
const normalizedPath = path.normalize(requestedPath); const normalizedPath = path.normalize(requestedPath);
const requestedRelativePath = path.relative(volumePath, normalizedPath); const requestedRelativePath = path.relative(volumePath, normalizedPath);
if (requestedRelativePath.startsWith("..") || path.isAbsolute(requestedRelativePath)) { if (
requestedRelativePath === ".." ||
requestedRelativePath.startsWith(`..${path.sep}`) ||
path.isAbsolute(requestedRelativePath)
) {
throw new Error("Invalid path"); throw new Error("Invalid path");
} }
@ -32,7 +36,7 @@ export const listVolumeFiles = async (
const realRequestedPath = await fs.realpath(requestedPath); const realRequestedPath = await fs.realpath(requestedPath);
const relative = path.relative(realVolumeRoot, realRequestedPath); const relative = path.relative(realVolumeRoot, realRequestedPath);
if (relative.startsWith("..") || path.isAbsolute(relative)) { if (relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative)) {
throw new Error("Invalid path"); throw new Error("Invalid path");
} }