feat(runs): add deleting files from a run

- add DELETE /scenarios/:id/runs/:runId/files/:fileId and the
  delete_run_file MCP tool; the stored file is removed once no scenario
  or run references it
- add a delete button with confirmation to the run detail files table
- bump version to 1.17.0
This commit is contained in:
Andrii Arsenin committed 2026-09-30 18:51:25 +03:00
1 parent 25a89caf96
commit 35293e372b
12 files changed
+249 -5

No files matched your search

+78
View File
@@ -6,6 +6,7 @@ import { Repository } from "typeorm";
import { buildTestApp } from "./app.harness";
import { FileStorageService } from "../src/file/file-storage.service";
import { FileEntity } from "../src/file/file.entity";
import { ScenarioRunFileEntity } from "../src/file/scenario-run-file.entity";
import { ScenarioEntity } from "../src/scenario/scenario.entity";
import { ScenarioRunEntity } from "../src/scenario/scenario-run.entity";
@@ -15,6 +16,7 @@ describe("File Controller Integration Tests", () => {
let fileRepo: Repository<FileEntity>;
let scenarioRepo: Repository<ScenarioEntity>;
let runRepo: Repository<ScenarioRunEntity>;
let runFileRepo: Repository<ScenarioRunFileEntity>;
let filesDir: string;
beforeAll(async () => {
@@ -27,6 +29,9 @@ describe("File Controller Integration Tests", () => {
runRepo = app.get<Repository<ScenarioRunEntity>>(
getRepositoryToken(ScenarioRunEntity),
);
runFileRepo = app.get<Repository<ScenarioRunFileEntity>>(
getRepositoryToken(ScenarioRunFileEntity),
);
// Get the files directory from the service
filesDir = fileStorageService.getAbsolutePath("");
@@ -335,6 +340,79 @@ describe("File Controller Integration Tests", () => {
});
});
// ── DELETE /scenarios/:id/runs/:runId/files/:fileId ───────────────────────
describe("DELETE /scenarios/:id/runs/:runId/files/:fileId", () => {
async function createArtifact(runId: string, name = "to-delete.txt") {
return fileStorageService.createAndSaveRunArtifact(
runId,
Buffer.from("delete me"),
name,
"text/plain",
);
}
it("removes the file from the run and deletes the stored file", async () => {
const scenario = await createScenario("run-file-delete");
const run = await createRun(scenario.id);
const file = await createArtifact(run.id);
const physicalPath = fileStorageService.getAbsolutePath(file.filePath);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(204);
const list = await request(app.getHttpServer())
.get(`/scenarios/${scenario.id}/runs/${run.id}/files`)
.expect(200);
expect(list.body.items).toHaveLength(0);
expect(await fileRepo.findOneBy({ id: file.id })).toBeNull();
await expect(fs.access(physicalPath)).rejects.toThrow();
});
it("keeps the stored file while another run still references it", async () => {
const scenario = await createScenario("run-file-delete-shared");
const run = await createRun(scenario.id);
const otherRun = await createRun(scenario.id);
const file = await createArtifact(run.id, "shared.txt");
await runFileRepo.save(
runFileRepo.create({ runId: otherRun.id, fileId: file.id }),
);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(204);
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
await request(app.getHttpServer())
.get(
`/scenarios/${scenario.id}/runs/${otherRun.id}/files/${file.id}/content`,
)
.expect(200);
});
it("returns 404 for a file that belongs to a different run", async () => {
const scenario = await createScenario("run-file-delete-mismatch");
const run = await createRun(scenario.id);
const otherRun = await createRun(scenario.id);
const file = await createArtifact(otherRun.id);
await request(app.getHttpServer())
.delete(`/scenarios/${scenario.id}/runs/${run.id}/files/${file.id}`)
.expect(404);
expect(await fileRepo.findOneBy({ id: file.id })).not.toBeNull();
});
it("returns 404 when the run does not exist", async () => {
const scenario = await createScenario("run-file-delete-no-run");
await request(app.getHttpServer())
.delete(
`/scenarios/${scenario.id}/runs/00000000-0000-0000-0000-000000000000/files/00000000-0000-0000-0000-000000000000`,
)
.expect(404);
});
});
// ── File cleanup ───────────────────────────────────────────────────────────
describe("File cleanup", () => {
+36
View File
@@ -611,6 +611,42 @@ describe("McpController", () => {
});
});
describe("delete_run_file", () => {
it("deletes a run artifact so it is no longer listed", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete");
const listFiles = async () =>
JSON.parse(
(
(await mcpCall("list_run_files", { scenarioId: sc.id, runId: run.id }))
.rpc.result as { content: { text: string }[] }
).content[0].text,
) as { items: Array<{ id: string; name: string }>; total: number };
const file = (await listFiles()).items.find((f) => f.name === "artifact.bin")!;
const { status, rpc } = await mcpCall("delete_run_file", {
scenarioId: sc.id,
runId: run.id,
fileId: file.id,
});
expect(status).toBe(200);
const result = rpc.result as { isError?: boolean; content: { text: string }[] };
expect(result.isError).toBeFalsy();
expect(result.content[0].text).toContain(file.id);
expect((await listFiles()).items.map((f) => f.id)).not.toContain(file.id);
});
it("returns an MCP error for a file not in the run", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-delete-missing");
const { status, rpc } = await mcpCall("delete_run_file", {
scenarioId: sc.id,
runId: run.id,
fileId: "00000000-0000-0000-0000-000000000000",
});
expect(status).toBe(200);
expect((rpc.result as { isError: boolean }).isError).toBe(true);
});
});
describe("get_run_file_content", () => {
it("returns run artifact bytes as base64 for a valid file", async () => {
const { sc, run } = await createRunArtifactFixture("mcp-run-file-content-test");