Skip to content

Commit 2c99385

Browse files
vojtabiberleclaude
andcommitted
DMD-921 - Fix cleanup for pre-existing sample directories
For existing directories, remove old files before writing new ones to prevent mixed old/new state on partial failure. On any failure, clean up written files and only remove directory if we created it. Updated doc comment to accurately describe the cleanup behavior. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 28c0169 commit 2c99385

1 file changed

Lines changed: 21 additions & 7 deletions

File tree

internal/pkg/llm/twinformat/generator.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,8 @@ func (g *Generator) generateSamplesIndex(ctx context.Context, data *ProcessedDat
13171317
}
13181318

13191319
// generateSampleFile generates a sample CSV file and metadata for a table.
1320-
// On failure after creating the directory, cleans up to avoid partial artifacts.
1320+
// For new directories, cleans up on failure to avoid partial artifacts.
1321+
// For existing directories, removes old files before writing to prevent mixed state.
13211322
func (g *Generator) generateSampleFile(ctx context.Context, sample *TableSample) (err error) {
13221323
// Create table-specific directory.
13231324
tableDir := filesystem.Join(g.outputDir, "samples", sample.TableID.String())
@@ -1326,17 +1327,31 @@ func (g *Generator) generateSampleFile(ctx context.Context, sample *TableSample)
13261327
return errors.Errorf("failed to create sample directory: %w", err)
13271328
}
13281329

1329-
// Clean up on failure to avoid partial sample artifacts, but only if we created the directory.
1330+
csvPath := filesystem.Join(tableDir, "sample.csv")
1331+
metadataPath := filesystem.Join(tableDir, "metadata.json")
1332+
1333+
// If directory existed, remove old files to prevent mixed old/new state on partial failure.
1334+
if tableDirExistedBefore {
1335+
_ = g.fs.Remove(ctx, csvPath)
1336+
_ = g.fs.Remove(ctx, metadataPath)
1337+
}
1338+
1339+
// Clean up on failure to avoid partial sample artifacts.
13301340
defer func() {
1331-
if err != nil && !tableDirExistedBefore {
1332-
if removeErr := g.fs.Remove(ctx, tableDir); removeErr != nil {
1333-
g.logger.Warnf(ctx, "Failed to clean up partial sample directory %s: %v", tableDir, removeErr)
1341+
if err != nil {
1342+
// Remove any files we may have written.
1343+
_ = g.fs.Remove(ctx, csvPath)
1344+
_ = g.fs.Remove(ctx, metadataPath)
1345+
// Remove directory only if we created it.
1346+
if !tableDirExistedBefore {
1347+
if removeErr := g.fs.Remove(ctx, tableDir); removeErr != nil {
1348+
g.logger.Warnf(ctx, "Failed to clean up partial sample directory %s: %v", tableDir, removeErr)
1349+
}
13341350
}
13351351
}
13361352
}()
13371353

13381354
// Write CSV file.
1339-
csvPath := filesystem.Join(tableDir, "sample.csv")
13401355
if err = g.csvWriter.Write(ctx, csvPath, sample.Columns, sample.Rows); err != nil {
13411356
return errors.Errorf("failed to write sample CSV: %w", err)
13421357
}
@@ -1355,7 +1370,6 @@ func (g *Generator) generateSampleFile(ctx context.Context, sample *TableSample)
13551370
"table_id": sample.TableID.String(),
13561371
}
13571372

1358-
metadataPath := filesystem.Join(tableDir, "metadata.json")
13591373
if err = g.jsonWriter.Write(ctx, metadataPath, metadata); err != nil {
13601374
return err
13611375
}

0 commit comments

Comments
 (0)