{"record":{"id":"82935f5cb756f8af","repo":"charmbracelet/crush","slug":"error-creating-file-history-w-82935f","errorCode":null,"errorMessage":"error creating file history: %w","messagePattern":"error creating file history: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/agent/tools/write.go","lineNumber":148,"sourceCode":"\t\t\t\t\tDiff:      diff,\n\t\t\t\t\tAdditions: additions,\n\t\t\t\t\tRemovals:  removals,\n\t\t\t\t})\n\t\t\t\treturn resp, nil\n\t\t\t}\n\n\t\t\terr = os.WriteFile(filePath, []byte(params.Content), 0o644)\n\t\t\tif err != nil {\n\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"error writing file: %w\", err)\n\t\t\t}\n\n\t\t\t// Check if file exists in history\n\t\t\tfile, err := files.GetByPathAndSession(ctx, filePath, sessionID)\n\t\t\tif err != nil {\n\t\t\t\t_, err = files.Create(ctx, sessionID, filePath, oldContent)\n\t\t\t\tif err != nil {\n\t\t\t\t\t// Log error but don't fail the operation\n\t\t\t\t\treturn fantasy.ToolResponse{}, fmt.Errorf(\"error creating file history: %w\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t\tif file.Content != oldContent {\n\t\t\t\t// User manually changed the content; store an intermediate version\n\t\t\t\t_, err = files.CreateVersion(ctx, sessionID, filePath, oldContent)\n\t\t\t\tif err != nil {\n\t\t\t\t\tslog.Error(\"Error creating file history version\", \"error\", err)\n\t\t\t\t}\n\t\t\t}\n\t\t\t// Store the new version\n\t\t\t_, err = files.CreateVersion(ctx, sessionID, filePath, params.Content)\n\t\t\tif err != nil {\n\t\t\t\tslog.Error(\"Error creating file history version\", \"error\", err)\n\t\t\t}\n\n\t\t\tfiletracker.RecordRead(ctx, sessionID, filePath)\n\n\t\t\tnotifyLSPs(ctx, lspManager, params.FilePath)","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/agent/tools/write.go#L130-L166","documentation":"After a successful write, the tool records the file's prior content in a per-session file history (files.Create). If the record cannot be created (DB error, session not persisted, constraint failure), the tool returns this wrapped error — notably the file write itself has already succeeded, but the operation reports failure.","triggerScenarios":"files.Create fails when the session ID in context does not exist in the sessions table (FK/constraint), the SQLite database is locked or read-only, or the DB migration state is inconsistent.","commonSituations":"Running the tool with a fabricated/foreign session ID against the real DB; SQLite 'database is locked' under concurrent writers; disk full preventing DB writes; opening the DB read-only.","solutions":["Ensure the session ID in the context corresponds to a real persisted session.","Check SQLite lock contention / concurrent writers; retry or serialize writes.","Verify the database is writable and has disk space.","Run pending DB migrations to keep the schema consistent."],"exampleFix":"// before\nctx := agent.WithSession(context.Background(), \"nonexistent-session\")\ntool.Run(ctx, params)\n\n// after\nsess, _ := app.Sessions.Create(ctx, agent.DefaultSessionName)\nctx := agent.WithSession(ctx, sess.ID)\ntool.Run(ctx, params)","handlingStrategy":"retry","validationCode":"if sess, err := app.Sessions.Get(ctx, sessionID); err != nil {\n    return fmt.Errorf(\"session %s not persisted; history recording will fail\", sessionID)\n}","typeGuard":null,"tryCatchPattern":"if err != nil {\n    if strings.Contains(err.Error(), \"database is locked\") {\n        time.Sleep(100 * time.Millisecond)\n        // retry the tool call once\n    }\n    return err\n}","preventionTips":["Only use session IDs returned by Sessions.Create — never fabricated ones.","Enable SQLite busy_timeout / WAL mode to reduce lock errors.","Ensure the DB directory has free disk space and write permission."],"tags":["go","database","sqlite","session","file-history"],"backgroundTag":"database-write-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}