ISSUE DESCRIPTION marimo edit --watch messes with merge conflict markers Describe the…
- User
ISSUE DESCRIPTION
marimo edit --watch messes with merge conflict markers
Describe the bug
When cherry-picking commits, or
git stash poping, sometimes merge conflicts occur.If a notebook is open using
marimo edit --watchduring that time, it parses the git conflict markers asapp._unparsable_cell. This gets in the way of resolving the conflicts using conflict resolution tools likegit-mediateSuggestion: Avoid updating the notebook if the file contains conflict markers, recognizable by
<<<<<<<at the beginning of a line.Will you submit a PR?
- Yes
Environment
{ "marimo": "0.23.6", "editable": false, "location": "/Users/yairchu/dev/soundradix/Drum3D/.venv/lib/python3.13/site-packages/marimo", "OS": "Darwin", "OS Version": "25.2.0", "Processor": "arm", "Python Version": "3.13.1", "Locale": "C/en_US", "Binaries": { "Browser": "140.0.7339.134", "Node": "v23.11.0", "uv": "0.7.12 (dc3fd4647 2025-06-06)" }, "Dependencies": { "click": "8.4.0", "docutils": "0.22.4", "itsdangerous": "2.2.0", "jedi": "0.19.2", "markdown": "3.10.2", "narwhals": "2.21.2", "packaging": "26.2", "psutil": "7.2.2", "pygments": "2.20.0", "pymdown-extensions": "10.21.3", "pyyaml": "6.0.3", "starlette": "1.0.0", "tomlkit": "0.15.0", "typing-extensions": "4.15.0", "uvicorn": "0.47.0", "websockets": "16.0" }, "Optional Dependencies": { "altair": "6.1.0", "anywidget": "0.11.0", "loro": "1.10.3", "pandas": "3.0.3" }, "Experimental Flags": {} }Code to reproduce
No response
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you callsubmit) usinggit statusorgit diffcommands. However you don't need to usegit addorgit commitbefore submitting your changes. In fact,submitonly submits currently unstaged changes because it uses to get the changes that need to be submitted. Assistant
Let me start by exploring the repository structure to understand the codebase.
Assistant
Now let me find the FileChangeCoordinator:
Assistant
Now I understand the flow. Let me look at the
reloadmethod and how the file is being read:Assistant
Now I have a good understanding. Let me look at the
_load_appfunction and explore where the merge conflict markers might be getting parsed:Assistant
Let me look at the
load.load_appfunction:Assistant
Now let me see how the existing tests are organized for the file watcher integration:
Assistant
Now let me look at how the file watcher is set up in the export command to also understand the flow:
Assistant
Now I have a thorough understanding. Let me look at how the
readis performed in storage to see if it's reading from the file directly:Assistant
Now let me write a script to reproduce the issue first:
Assistant
I've reproduced the bug. The reload was triggered even when the file has merge conflict markers. Now let me fix this. The fix should be in the
FileChangeCoordinator(or somewhere) to check for merge conflict markers before reloading.Assistant
Now let me check what files exist in the module's directory for storing utilities:
Assistant
Let me see how to add a check for the merge conflict markers in the file:
Assistant
Now let me think about where to add the conflict marker check. The issue is that when the user is in the middle of resolving a conflict, marimo doesn't see it. The best approach would be to:
- Add a check before reloading in
FileChangeCoordinator._handle_file_change_locked. This is the central point where all file change events flow.
Let me look at how the file is read in the file_change_handler:
- Add a check before reloading in
Assistant
Let me also check what test for
test_file_content_matches_last_saveexists:Assistant
Now let me design the fix. I'll add a check in
FileChangeCoordinator._handle_file_change_lockedthat detects merge conflict markers in the file before reloading. I'll also need a helper to detect the markers.Let me think about the best approach. We need to:
- Read the file content
- Check if it contains merge conflict markers (lines starting with
<<<<<<<,=======,>>>>>>>) - If it does, skip the reload and log a warning
Let me look at what existing patterns are used:
Assistant
Now let me implement the fix. I'll add a check in the
FileChangeCoordinator._handle_file_change_lockedmethod:Assistant
Now let me implement the fix:
Assistant
Oops, I see there's a duplicated
LOGGER = _loggers.marimo_logger()line. Let me fix that:Assistant
I see I accidentally added two
if TYPE_CHECKING:blocks. Let me fix that:Assistant
Now let me view the relevant section of the file to add the conflict check:
Assistant
Now let me add the conflict marker check. I'll add it right after the
file_content_matches_last_savecheck:Assistant
There's duplicate content after line 280. Let me view the rest of the file and clean it up:
Assistant
I see - my edit caused a duplicate section. Let me clean that up:
Assistant
Let me verify the full file:
Assistant
Now let me run my reproduction script to confirm the fix:
Assistant
Looks like my edit removed too much. Let me see the rest:
Assistant
Let me add the function body:
Assistant
Let me run the test now:
Assistant
The fix is working. Now let me also test that normal reloads still work:
Assistant
Now let me test more edge cases:
Assistant
All edge cases are passing. Now let me run the existing tests to make sure nothing was broken:
Assistant
Hmm, that test was actually broken already before my fix. Let me check if this test was failing before my fix:
Assistant
Interesting! That test passed without my fix. Let me check the test more carefully to understand what's happening:
Assistant
I see - so
handle_changeis supposed to skip when paths don't match. Let me trace through this. With my change,_handle_file_change_lockedis called with the absolute path ofother_file. Let me trace it:Assistant
Let me think about this. I think the issue is that my function is being called with the absolute path, but
app_file_manager.pathmay have the same canonical form. Let me trace through the test:test_file = tmp_path / "test.py"=> e.g./tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/test.pyother_file = tmp_path / "other.py"=> e.g./tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/other.pymock_session.app_file_manager = AppFileManager(filename=str(test_file))=>app_file_manager.path = /tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/test.pyawait coordinator.handle_change(other_file, session=mock_session):abs_file_path = await async_path.abspath(other_file)=>/tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/other.py_handle_file_change_locked(abs_file_path, session):session.app_file_manager.path != file_path=>/tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/test.py != /tmp/pytest-of-root/pytest-0/test_file_change_coordinator_p1/other.py
Assistant
Interesting - it's returning handled=True. Let me check what
abs_file_pathends up being:Assistant
So the path mismatch check should fire. Let me check what's happening - maybe I'm calling the wrong handle_change path:
Assistant
Interesting! It seems that even without my changes,
afm.pathmight be returning something different from what I expected. Let me check thepathproperty:Assistant
OK so the path comparison should work. Let me see what file content the file_change_handler actually processes:
Assistant
Hmm! That's strange. The comparison says they're different but the result says
handled=True. Let me check ifapp_file_manager.pathis actually being called from the session:Wait, this is interesting - the mock_session is a
MagicMock. Looking at the test,mock_session.app_file_manageris set to the realafm, but maybe the test path usesmock_session.app_file_manager.pathand gets the auto-mocked value because of how MagicMock handles attribute access?Let me check:
Assistant
OK, the mock_session does return the right thing. So why is
_handle_file_change_lockedreturninghandled=True?Let me add some debugging to the code temporarily:
Assistant
I see!
afm.reload()is loadingtest_file(its own file), notother_filebecausereload()usesself.pathwhich istest_file. So even when paths don't match,reload()succeeds. So the test was relying on thepath != file_pathcheck to fail, and my fix doesn't affect that.Wait, let me re-run the test to confirm:
Assistant
Hmm, this is failing now. Let me check if it's failing before my fix:
Assistant
So the test passes without my fix but fails with it. Let me investigate. Maybe there's something subtle with my changes. Let me re-check the file:
Assistant
Wait, I don't see the path mismatch check anymore! Let me look at the git diff:
Assistant
Oh no, I accidentally removed the path mismatch check! That's because my edit was at lines 219:240 but I removed lines 213-219 too (the path mismatch check). Let me fix this:
Assistant
Now let me verify the diff looks clean:
Assistant
The diff looks clean. Let me run the tests now:
Assistant
All 23 tests pass. Let me also confirm my test still works:
Assistant
All edge cases still pass. Let me also run other relevant tests:
Assistant
This test failure is unrelated to my changes - it's about process killing. Let me skip it and run the other tests:
Assistant
All session tests pass except for that unrelated kernel test. Let me also run the tests for
_server/test_file_manager.py:Assistant
These failures are also unrelated to my changes. Let me check by running the original tests:
Assistant
Good - these failures are pre-existing and unrelated to my fix. Let me check for the test that I should also consider, the file_watcher_integration tests:
Assistant
Now let me also check if my fix could have edge cases where it would not behave as expected. Let me check the unicode merge conflict markers that may also exist:
Assistant
Wait, my test was wrong - 8
=is still 7=at the start. Let me re-test that:Assistant
Hmm, the regex is matching too much. Let me look at my regex:
Assistant
OK so the regex doesn't anchor at the end. That's intentional behavior because conflict markers are typically followed by something like
HEADorbranch-name. So 9=would still match because it starts with 7=. This is actually correct behavior - we want to detect any line that starts with the conflict marker.Wait, but in my actual test for that case I'm asserting that 9
=should NOT match. That's wrong. Let me re-think. Actually, when someone writes 9=in a notebook, it could be:- Actual conflict marker followed by something - rare but possible
- A horizontal separator in markdown
But it's actually fine for the regex to match - my regex isn't strict, but it will trigger the conflict detection which would skip the reload. This is acceptable behavior because:
- If a notebook has a line that just happens to start with
=======, it gets skipped - The skip is only for file watching - the user can still manually reload
- This is a very rare case
Actually, looking again, I'm being too strict. Let me look at what the regex matches and consider whether to tighten it:
Assistant
Hmm. So we get false positives on
==========. But in practice this is unlikely to occur in a notebook. Plus, it's a tradeoff between false positives and false negatives. False negatives are worse (we corrupt the user's conflict markers) so we lean toward false positives.However, I can tighten the regex to require exactly 7 characters followed by a space or end of line. Let me look at what
gititself does:Git's actual conflict markers are:
<<<<<<<(7<followed by space) — start of "ours"|||||||(7|followed by space) — start of ancestor in diff3
Assistant
Now let me also check what happens if the marker is at end of file without newline:
Assistant
Now let me update the regex in the file to be more strict. Let me think about which approach to take. There's a tradeoff between simplicity and accuracy.
Actually, let me reconsider. The original approach with
r"^(?:<{7}|={7}|>{7}|\|{7})"is simpler and has the property that:- It will never miss a conflict marker
- It may produce false positives for unusual separator lines
The stricter approach
r"^(?:<{7}|>{7}|\|{7}) |^={7}$"requires:- Start markers (
<,>,|) must be followed by a space - The
=separator must be exactly 7=characters at end of line
I think the stricter approach is better because:
- It matches git's actual output format
- It avoids false positives on common separator lines
- Real conflict markers always follow this format
Let me update:
Assistant
Now I need to update the
_has_merge_conflict_markersfunction to use the new regex correctly. Let me also think about using the regex insearchmode rather thanmatchon each line:Assistant
Wait, I have a bug. The
MULTILINEflag is only useful when using^and$insearchmode over a multiline string. When usingmatchon a single line,$matches end of string (which is also end of line becausematchis constrained to start of string). Let me check:Actually,
$without MULTILINE matches end of string. Whenmatch()is used on a single line (without trailing newline),$matches at end of line. With trailing newline,$would still need to match wherelineends - let me think...Actually, when iterating over a file line by line, the
lineincludes the trailing newline. Somatchis anchored at position 0, and$matches at the end of the string (which is end of line + newline). So^={7}$should work correctly.Let me test:
Assistant
The regex is working correctly. Now let me run my test scripts again:
Assistant
Let me also test that the regex is now strict enough to not match common separator patterns:
Assistant
All edge cases are working correctly. Let me also run the existing tests:
Assistant
All 23 tests pass. Let me also check that I'm checking for the right file format. Let me think about markdown notebooks too:
Assistant
Markdown files also work correctly. Let me also test the case where the file doesn't exist:
Assistant
Now let me also verify the behavior for unicode:
Assistant
All edge cases handled. Let me look at the final state of the file:
Assistant
Let me also look at the final diff:
Assistant
The diff is clean. Now let me clean up the test files and verify my final solution:
Assistant
Let me run the full session test suite to confirm nothing is broken:
Assistant
339 tests pass, only 1 xfail which is expected.
Let me also re-confirm the original repro works:
Assistant
The fix is working correctly. Let me run a broader test to make sure I haven't broken anything:
Assistant
The single failure is unrelated to my changes (it's about OpenAI providers). Let me run the rest without it:
Assistant
There are a number of test failures, but they look unrelated to my changes. Let me check by stashing my changes and running the same set of tests:
Assistant
The same 87 failures occur without my changes, so they are pre-existing. The pytest run modified some snapshot files. Let me revert those and pop the stash:
Assistant
The snapshot files are not relevant to my change. The pre-existing failures are unrelated to my changes. Let me verify my change is the only modification:
Assistant
My change is clean and contained to the single file. Let me run the relevant tests one final time to confirm:
Assistant
All 36 tests pass. Let me also check the file_manager test:
Assistant
Only the 2 pre-existing failures (which fail without my changes too). Let me also verify by checking my final reproduction test:
Assistant
Excellent! All scenarios pass. Let me also do a final check of the changes:
Assistant
Now let me go through the checklist:
Checklist
1. If an error was reported in the issue description, I have successfully reproduced it.
- ✅ YES. I created a script that reproduced the bug: when a notebook contains merge conflict markers, was still triggering a reload, which would parse the conflict markers as .
Assistant
The file is in good state. Let me view the final version of the file:
Assistant
Everything looks great. Let me submit my fix:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.