Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agentic-ide): close data-loss races and a .git symlink hole in the code editor

rubenluetke10-beep452d06dd84d8
diff --git a/jarvis/agentic_ide/file_editing.py b/jarvis/agentic_ide/file_editing.py
index ace953671e..94ebb0fa3f 100644
--- a/jarvis/agentic_ide/file_editing.py
+++ b/jarvis/agentic_ide/file_editing.py
@@ -108,11 +108,36 @@ def _normalise_relative(path: str) -> str:
parts = [part for part in pure.parts if part not in ("", ".")]
if not parts or any(part == ".." for part in parts):
raise EditError("That path leaves the workspace folder.")
- if any(part.lower() == ".git" for part in parts):
- raise EditError("Files inside .git cannot be edited here.")
+ _refuse_git(parts)
+ if os.name == "nt" and any(":" in part for part in parts):
+ # An NTFS alternate data stream (``a.txt:hidden``) is not a file to edit.
+ raise EditError("That is not a valid file name here.")
return "/".join(parts)
+def _refuse_git(parts: list[str] | tuple[str, ...]) -> None:
+ """Refuse a path inside git's own database.
+
+ Windows drops trailing dots and spaces from a name, so ``.git.`` opens
+ ``.git``; compare the name as the file system will read it.
+ """
+ if any(part.rstrip(". ").lower() == ".git" for part in parts):
+ raise EditError("Files inside .git cannot be edited here.")
+
+
+def _check_resolved(root: str | os.PathLike[str], target: Path) -> None:
+ """Refuse a target that only reaches ``.git`` through a symlink.
+
+ ``hooks -> .git/hooks`` passes the textual check and still stays inside the
+ workspace, but a write there is a git hook, i.e. code that runs later.
+ """
+ real_root = Path(os.path.realpath(os.fspath(root)))
+ try:
+ _refuse_git(target.relative_to(real_root).parts)
+ except ValueError as exc: # contained_path already guarantees this
+ raise EditError("That path leaves the workspace folder.") from exc
+
+
def _resolve(root: str | os.PathLike[str], path: str) -> tuple[str, Path]:
"""The normalised relative path and its contained absolute location."""
relative = _normalise_relative(path)
@@ -122,6 +147,7 @@ def _resolve(root: str | os.PathLike[str], path: str) -> tuple[str, Path]:
raise EditError("That path leaves the workspace folder.") from exc
if target == Path(os.path.realpath(os.fspath(root))):
raise EditError("Give a file path inside the workspace.")
+ _check_resolved(root, target)
return relative, target
@@ -139,6 +165,7 @@ def _resolve_entry(root: str | os.PathLike[str], path: str) -> tuple[str, Path]:
folder = contained_path(root, parent or ".")
except UnsafePathError as exc:
raise EditError("That path leaves the workspace folder.") from exc
+ _check_resolved(root, folder / name)
return relative, folder / name
@@ -175,11 +202,17 @@ def file_version(root: str | os.PathLike[str], path: str) -> str | None:
too large to edit reports an empty version, as :func:`read_text_file` does.
"""
_, target = _resolve(root, path)
- if not target.is_file():
+ try:
+ if target.stat().st_size > MAX_EDITABLE_BYTES:
+ return ""
+ return _version_of(target.read_bytes())
+ except (FileNotFoundError, IsADirectoryError, NotADirectoryError):
return None
- if target.stat().st_size > MAX_EDITABLE_BYTES:
- return ""
- return _version_of(target.read_bytes())
+ except PermissionError:
+ # Windows reports reading a folder as "access denied".
+ if target.is_dir():
+ return None
+ raise
def _atomic_write(target: Path, data: bytes) -> None:
@@ -278,8 +311,14 @@ def rename_entry(root: str | os.PathLike[str], source: str, destination: str) ->
if not (source_path.is_symlink() or source_path.exists()):
raise EditError("That file or folder no longer exists.")
# On a case-insensitive disk "readme.md" -> "README.md" names the same
- # entry; that is a rename, not a collision.
- same_entry = target.exists() and os.path.samefile(source_path, target)
+ # entry; that is a rename, not a collision. Only a change of case counts:
+ # a hard-linked sibling is also "the same file" and must not be replaced.
+ same_entry = (
+ target.exists()
+ and source_path.parent == target.parent
+ and source_path.name.lower() == target.name.lower()
+ and os.path.samefile(source_path, target)
+ )
if (target.exists() or target.is_symlink()) and not same_entry:
raise EditError("Something with that name already exists.")
source_key = os.path.normcase(str(source_path))
@@ -344,6 +383,7 @@ def list_files(root: str | os.PathLike[str]) -> tuple[list[str], bool]:
base = Path(os.path.realpath(os.fspath(root)))
listed = _git_listed_files(base)
if listed is None:
+ logger.debug("Quick Open: no git listing for {}, walking the folder", base.name)
return _walked_files(base)
return listed[:MAX_LISTED_FILES], len(listed) > MAX_LISTED_FILES
diff --git a/jarvis/agentic_ide/git_changes.py b/jarvis/agentic_ide/git_changes.py
index 7b0628101d..70099c8946 100644
--- a/jarvis/agentic_ide/git_changes.py
+++ b/jarvis/agentic_ide/git_changes.py
@@ -308,7 +308,8 @@ def head_text(folder: str | Path, path: str) -> str | None:
result = _git(["show", f"HEAD:./{rel}"], root)
if result is None or result.returncode != 0:
return None
- return result.stdout
+ # The editor shows a BOM file's text without its BOM; so must the diff.
+ return result.stdout.removeprefix("")
def file_diff(folder: str | Path, path: str) -> FileDiff:
diff --git a/jarvis/ui/web/agentic_ide_routes.py b/jarvis/ui/web/agentic_ide_routes.py
index ccbb55ee90..f9daacd0e6 100644
--- a/jarvis/ui/web/agentic_ide_routes.py
+++ b/jarvis/ui/web/agentic_ide_routes.py
@@ -2008,8 +2008,13 @@ async def get_workspace_text_file_version(workspace_id: str, path: str) -> dict[
version = await asyncio.to_thread(file_editing.file_version, folder, path)
except file_editing.EditError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
- except OSError:
- version = None
+ except OSError as exc:
+ # A locked or unreadable file is not a deleted one: answer "try again"
+ # so the editor keeps the file as it is instead of marking it deleted.
+ log.debug("Agentic IDE editor: version check failed for %s: %s", path, exc)
+ raise HTTPException(
+ status_code=503, detail="The file could not be read right now."
+ ) from exc
return {"workspace_id": workspace_id, "path": path.replace("\\", "/"), "version": version}
diff --git a/jarvis/ui/web/frontend/src/components/agentic/editor/CodeEditorStage.tsx b/jarvis/ui/web/frontend/src/components/agentic/editor/CodeEditorStage.tsx
index afedbc23c0..bde3fc58e5 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/editor/CodeEditorStage.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/editor/CodeEditorStage.tsx
@@ -112,6 +112,10 @@ export function CodeEditorStage({
const key = event.key.toLowerCase();
const state = useCodeEditorStore.getState();
if (key === "p" && !event.shiftKey) {
+ // A text field outside the editor (a rename box, the chat composer)
+ // keeps its keys; the editor's own text area does not count as one.
+ const field = target?.closest("input, textarea, [contenteditable='true']");
+ if (field && !stage.current?.contains(field)) return;
event.preventDefault();
event.stopPropagation();
state.setQuickOpen(true);
diff --git a/jarvis/ui/web/frontend/src/components/agentic/editor/editorModels.ts b/jarvis/ui/web/frontend/src/components/agentic/editor/editorModels.ts
index f22b797489..b26c20bfd4 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/editor/editorModels.ts
+++ b/jarvis/ui/web/frontend/src/components/agentic/editor/editorModels.ts
@@ -36,6 +36,14 @@ let api: MonacoApi | null = null;
const entries = new Map<string, Entry>();
const heads = new Map<string, Monaco.editor.ITextModel>();
const viewStates = new Map<string, Monaco.editor.ICodeEditorViewState | null>();
+const headLoads = new Map<string, Promise<Monaco.editor.ITextModel | null>>();
+/**
+ * Bumped when a save starts and when it ends. A disk check that began before
+ * a save may come back with the version the save just replaced; comparing the
+ * epoch before and after the request drops such a stale answer.
+ */
+const saveEpochs = new Map<string, number>();
+const bumpEpoch = (fileKey: string) => saveEpochs.set(fileKey, (saveEpochs.get(fileKey) ?? 0) + 1);
export function setMonaco(monaco: MonacoApi): void {
api = monaco;
@@ -128,6 +136,8 @@ function disposeFile(fileKey: string): void {
}
heads.get(fileKey)?.dispose();
heads.delete(fileKey);
+ headLoads.delete(fileKey);
+ saveEpochs.delete(fileKey);
for (const key of [...viewStates.keys()]) if (key.endsWith(`:${fileKey}`)) viewStates.delete(key);
}
@@ -198,21 +208,33 @@ export async function loadFile(fileKey: string): Promise<void> {
}
/** The committed text a diff tab compares against; empty for a new file. */
-export async function loadHead(fileKey: string): Promise<Monaco.editor.ITextModel | null> {
- const file = store().files[fileKey];
- if (!file || !api) return null;
- const cached = heads.get(fileKey);
- if (cached) return cached;
- let text: string | null = null;
- try {
- text = await fetchHeadText(file.workspaceId, file.path);
- } catch (error) {
- toast((error as Error).message);
- }
- if (!store().files[fileKey]) return null;
- const model = api.editor.createModel(text ?? "", languageFor(file.path), uriFor(file.workspaceId, file.path, "jarvis-head"));
- heads.set(fileKey, model);
- return model;
+export function loadHead(fileKey: string): Promise<Monaco.editor.ITextModel | null> {
+ // One request per file at a time: two diff tabs switching back and forth
+ // must not both create the same model. Each new showing re-reads the commit,
+ // so a commit made meanwhile shows up.
+ const pending = headLoads.get(fileKey);
+ if (pending) return pending;
+ const load = (async () => {
+ const file = store().files[fileKey];
+ if (!file || !api) return null;
+ let text: string | null = null;
+ try {
+ text = await fetchHeadText(file.workspaceId, file.path);
+ } catch (error) {
+ toast((error as Error).message);
+ }
+ if (!store().files[fileKey]) return null;
+ const existing = heads.get(fileKey);
+ if (existing) {
+ if (existing.getValue() !== (text ?? "")) existing.setValue(text ?? "");
+ return existing;
+ }
+ const model = api.editor.createModel(text ?? "", languageFor(file.path), uriFor(file.workspaceId, file.path, "jarvis-head"));
+ heads.set(fileKey, model);
+ return model;
+ })().finally(() => headLoads.delete(fileKey));
+ headLoads.set(fileKey, load);
+ return load;
}
/**
@@ -229,6 +251,7 @@ export async function saveFile(fileKey: string, { overwrite = false } = {}): Pro
const expected = overwrite ? (file.conflict?.diskVersion ?? null) : file.version;
const create = overwrite ? file.conflict?.diskVersion == null : file.deleted || file.version === null;
const sent = entry.model.getAlternativeVersionId();
+ bumpEpoch(fileKey);
store().patchFile(fileKey, { saving: true });
try {
const saved = await saveTextFile(file.workspaceId, {
@@ -239,6 +262,7 @@ export async function saveFile(fileKey: string, { overwrite = false } = {}): Pro
create,
});
entry.saved = sent;
+ bumpEpoch(fileKey);
store().patchFile(fileKey, {
saving: false,
version: saved.version,
@@ -249,6 +273,7 @@ export async function saveFile(fileKey: string, { overwrite = false } = {}): Pro
});
return true;
} catch (error) {
+ bumpEpoch(fileKey);
if (error instanceof SaveConflictError) {
store().patchFile(fileKey, { saving: false, conflict: { diskVersion: error.currentVersion } });
} else {
@@ -264,17 +289,29 @@ export async function saveAll(workspaceId: string): Promise<void> {
for (const [key] of dirty) await saveFile(key);
}
-/** Throw the buffer away and show what is on disk now. */
-export async function revertFile(fileKey: string): Promise<void> {
+/**
+ * Throw the buffer away and show what is on disk now.
+ *
+ * `follow` is the quiet reload of a clean buffer after an agent's edit: if the
+ * user starts typing while the file is being read, their edit wins and the
+ * disk version turns into a conflict instead of replacing what they typed.
+ */
+export async function revertFile(fileKey: string, { follow = false } = {}): Promise<void> {
const file = store().files[fileKey];
const entry = entries.get(fileKey);
if (!file || !entry) return;
+ const before = entry.model.getAlternativeVersionId();
try {
const loaded = await loadTextFile(file.workspaceId, file.path);
+ if (entries.get(fileKey) !== entry) return;
if (loaded.text === null) {
toast("The file on disk can no longer be edited here.");
return;
}
+ if (follow && entry.model.getAlternativeVersionId() !== before) {
+ store().patchFile(fileKey, { conflict: { diskVersion: loaded.version } });
+ return;
+ }
replaceFromDisk(entry, loaded.text, loaded.eol);
store().patchFile(fileKey, {
version: loaded.version,
@@ -304,6 +341,7 @@ export function discardFile(fileKey: string): void {
export async function checkDisk(fileKey: string): Promise<void> {
const before = store().files[fileKey];
if (!before || before.saving || before.status === "loading" || before.status === "error") return;
+ const epoch = saveEpochs.get(fileKey) ?? 0;
let version: string | null;
try {
version = await fetchFileVersion(before.workspaceId, before.path);
@@ -311,7 +349,8 @@ export async function checkDisk(fileKey: string): Promise<void> {
return; // the next check asks again; a failed poll changes nothing
}
const file = store().files[fileKey];
- if (!file || file.saving) return;
+ // A save started or finished meanwhile: this answer may predate it.
+ if (!file || file.saving || (saveEpochs.get(fileKey) ?? 0) !== epoch) return;
if (version === null) {
if (!file.deleted) store().patchFile(fileKey, { deleted: true });
return;
@@ -321,7 +360,7 @@ export async function checkDisk(fileKey: string): Promise<void> {
await loadFile(fileKey);
return;
}
- if (!file.dirty) await revertFile(fileKey);
+ if (!file.dirty) await revertFile(fileKey, { follow: true });
else if (file.conflict?.diskVersion !== version) store().patchFile(fileKey, { conflict: { diskVersion: version } });
}
diff --git a/jarvis/ui/web/frontend/src/components/agentic/sidePanel/explorer/ExplorerPanel.tsx b/jarvis/ui/web/frontend/src/components/agentic/sidePanel/explorer/ExplorerPanel.tsx
index d98c4e4c74..2c6e46e4e5 100644
--- a/jarvis/ui/web/frontend/src/components/agentic/sidePanel/explorer/ExplorerPanel.tsx
+++ b/jarvis/ui/web/frontend/src/components/agentic/sidePanel/explorer/ExplorerPanel.tsx
@@ -636,6 +636,14 @@ function FileTree({
const rename = async (path: string, name: string) => {
setRenaming(null);
+ const saving = Object.values(useCodeEditorStore.getState().files).some(
+ (file) => file.workspaceId === workspaceId && file.saving && isUnder(file.path, path),
+ );
+ // A save in flight would land under the old name; wait for it.
+ if (saving) {
+ pushToast("error", t("ide_side_panel.explorer.busy_saving"));
+ return;
+ }
const destination = joinPath(parentPath(path), name);
try {
const moved = await moveEntry(workspaceId, path, destination);
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/de.json b/jarvis/ui/web/frontend/src/i18n/locales/de.json
index 809bcba657..a0eacadca5 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/de.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/de.json
@@ -6223,6 +6223,7 @@
"delete_body": "Es landet im Papierkorb und lässt sich von dort wiederherstellen.",
"delete_permanent_body": "Dieser Computer hat keinen Papierkorb. Endgültiges Löschen lässt sich nicht rückgängig machen.",
"delete_permanent": "Endgültig löschen",
+ "busy_saving": "Eine Datei darin wird noch gespeichert. Versuche es gleich noch einmal.",
"cancel": "Abbrechen"
},
"tabs": {
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/en.json b/jarvis/ui/web/frontend/src/i18n/locales/en.json
index 8c064cbda7..bfc2090247 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/en.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/en.json
@@ -6256,6 +6256,7 @@
"delete_body": "It goes to the trash, so you can still restore it.",
"delete_permanent_body": "This computer has no trash to move it to. A permanent delete cannot be undone.",
"delete_permanent": "Delete permanently",
+ "busy_saving": "A file in there is still saving. Try again in a moment.",
"cancel": "Cancel"
},
"tabs": {
diff --git a/jarvis/ui/web/frontend/src/i18n/locales/es.json b/jarvis/ui/web/frontend/src/i18n/locales/es.json
index 74cfbbcb3c..39a91bb242 100644
--- a/jarvis/ui/web/frontend/src/i18n/locales/es.json
+++ b/jarvis/ui/web/frontend/src/i18n/locales/es.json
@@ -6223,6 +6223,7 @@
"delete_body": "Irá a la papelera, así que aún podrás restaurarlo.",
"delete_permanent_body": "Este equipo no tiene papelera. Una eliminación definitiva no se puede deshacer.",
"delete_permanent": "Eliminar definitivamente",
+ "busy_saving": "Un archivo de ahí aún se está guardando. Inténtalo de nuevo en un momento.",
"cancel": "Cancelar"
},
"tabs": {
diff --git a/tests/unit/agentic_ide/test_file_editing.py b/tests/unit/agentic_ide/test_file_editing.py
index d3d6095878..b8bebcd5d7 100644
--- a/tests/unit/agentic_ide/test_file_editing.py
+++ b/tests/unit/agentic_ide/test_file_editing.py
@@ -113,6 +113,31 @@ def test_symlink_escape_is_refused(tmp_path: Path) -> None:
assert not (outside / "evil.txt").exists()
+def test_git_database_is_refused_by_any_spelling_or_link(tmp_path: Path) -> None:
+ hooks = tmp_path / ".git" / "hooks"
+ hooks.mkdir(parents=True)
+ with pytest.raises(EditError):
+ create_entry(tmp_path, ".git./hooks/pre-commit", directory=False)
+ try:
+ os.symlink(hooks, tmp_path / "hooks", target_is_directory=True)
+ except (OSError, NotImplementedError):
+ pytest.skip("this machine cannot create symlinks")
+ with pytest.raises(EditError):
+ write_text_file(tmp_path, "hooks/pre-commit", "evil", expected_version=None, create=True)
+ assert not (hooks / "pre-commit").exists()
+
+
+def test_rename_never_replaces_a_hard_linked_sibling(tmp_path: Path) -> None:
+ (tmp_path / "a.txt").write_text("x", encoding="utf-8")
+ try:
+ os.link(tmp_path / "a.txt", tmp_path / "b.txt")
+ except (OSError, NotImplementedError):
+ pytest.skip("this file system has no hard links")
+ with pytest.raises(EditError):
+ rename_entry(tmp_path, "a.txt", "b.txt")
+ assert (tmp_path / "a.txt").exists()
+
+
def test_create_rename_and_permanent_delete(tmp_path: Path) -> None:
assert create_entry(tmp_path, "src/pkg", directory=True) == "src/pkg"
assert create_entry(tmp_path, "src/pkg/mod.py", directory=False) == "src/pkg/mod.py"
@@ -229,6 +254,20 @@ async def test_version_route_tracks_changes_and_deletion(workspace: Path) -> Non
assert gone["version"] is None
+async def test_version_route_reports_an_unreadable_file_as_busy_not_deleted(
+ workspace: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ (workspace / "a.txt").write_text("one\n", encoding="utf-8")
+
+ def locked(root: object, path: str) -> str:
+ raise PermissionError("held by another process")
+
+ monkeypatch.setattr(file_editing, "file_version", locked)
+ with pytest.raises(HTTPException) as caught:
+ await routes.get_workspace_text_file_version("workspace-1", "a.txt")
+ assert caught.value.status_code == 503
+
+
async def test_routes_reject_unknown_workspace_and_escapes(workspace: Path) -> None:
with pytest.raises(HTTPException) as unknown:
await routes.get_workspace_text_file("nope", "main.py")