charmbracelet/crush · error

error creating file history: %w

Error message

error creating file history: %w

What it means

The file was written successfully, but recording it in the file-history store (edit.files.Create) failed, so the change isn't associated with the session's history. This is a persistence/bookkeeping failure, not an edit failure — the file content on disk is correct.

Source

Thrown at internal/agent/tools/multiedit.go:224

			NewContent:   currentContent,
			Additions:    additions,
			Removals:     removals,
			EditsApplied: editsApplied,
			EditsFailed:  failedEdits,
		})
		return resp, nil
	}

	// Write the file
	err = os.WriteFile(params.FilePath, []byte(currentContent), 0o644)
	if err != nil {
		return fantasy.ToolResponse{}, fmt.Errorf("failed to write file: %w", err)
	}

	// Update file history
	_, err = edit.files.Create(edit.ctx, sessionID, params.FilePath, "")
	if err != nil {
		return fantasy.ToolResponse{}, fmt.Errorf("error creating file history: %w", err)
	}

	_, err = edit.files.CreateVersion(edit.ctx, sessionID, params.FilePath, currentContent)
	if err != nil {
		slog.Error("Error creating file history version", "error", err)
	}

	edit.filetracker.RecordRead(edit.ctx, sessionID, params.FilePath)

	var message string
	if len(failedEdits) > 0 {
		message = fmt.Sprintf("File created with %d of %d edits: %s (%d edit(s) failed)", editsApplied, len(params.Edits), params.FilePath, len(failedEdits))
	} else {
		message = fmt.Sprintf("File created with %d edits: %s", len(params.Edits), params.FilePath)
	}
	message = withWhitespaceNote(message, whitespaceCorrected)

	return fantasy.WithResponseMetadata(

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Check the wrapped error — verify the SQLite DB file exists and is writable
  2. Confirm the session ID refers to an existing session row
  3. Retry after the DB lock clears; check for other long-running writers
  4. If the DB is corrupt, restore from backup; the written file itself is unaffected
Defensive patterns

Strategy: retry

Validate before calling

// before the operation, verify DB reachable
if err := db.PingContext(ctx); err != nil { return err }
// and session exists
if _, err := queries.GetSession(ctx, sessionID); err != nil { return err }

Try / catch

if err := edit.files.Create(ctx, sessionID, path, ""); err != nil {
    if errors.Is(err, sqlite.ErrLocked) || errors.Is(err, sqlite.ErrBusy) {
        time.Sleep(backoff) // retry
    }
    slog.Error("Error creating file history", "error", err)
}

Prevention

When it happens

Trigger: files.Create(ctx, sessionID, path, "") returns an error, typically because the SQLite database is unavailable, the session row doesn't exist (foreign-key constraint), or the DB is locked/corrupted.

Common situations: Database file deleted or on a read-only mount mid-session; session created outside the DB; concurrent writers holding the SQLite lock past the busy timeout; migration state mismatch.

Related errors


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/04c92520fad4e6cb. Report an issue: GitHub.