Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(agent-core): join file workers on cancel, one \n line model for diffs, bash over Agent.jobs

cyhb0d0340f331f
diff --git a/core/agent_core/tools/edit.py b/core/agent_core/tools/edit.py
index 8a74c3c09..f13335abb 100644
--- a/core/agent_core/tools/edit.py
+++ b/core/agent_core/tools/edit.py
@@ -24,6 +24,7 @@ from core.agent_core.tools.paths import (
read_at_most,
resolve_to_cwd,
target_kind,
+ to_thread_joined,
)
from core.agent_core.tools.text import decode_file, encode_file, model_text
from core.agent_core.tools.truncate import format_size
@@ -183,7 +184,7 @@ class EditTool:
if not os.access(absolute, os.R_OK | os.W_OK):
return error_result(f"Could not edit file: {path}. Error code: EACCES.")
try:
- size, data, base, new_content = await asyncio.to_thread(_plan_edits, absolute, edits, path)
+ size, data, base, new_content = await to_thread_joined(_plan_edits, absolute, edits, path)
if len(data) > MAX_EDIT_BYTES:
return error_result(
f"File {path} would be {format_size(len(data))} after this edit, over the "
@@ -192,7 +193,7 @@ class EditTool:
)
if ctx.cancel.cancelled:
return error_result("Operation aborted")
- await asyncio.to_thread(write_bytes, absolute, data)
+ await to_thread_joined(write_bytes, absolute, data)
except _TooLarge as exc:
# The whole file and several copies would sit in the process every Session shares.
return error_result(
diff --git a/core/agent_core/tools/edit_diff.py b/core/agent_core/tools/edit_diff.py
index b6e83b1f1..41756c03f 100644
--- a/core/agent_core/tools/edit_diff.py
+++ b/core/agent_core/tools/edit_diff.py
@@ -330,7 +330,7 @@ def display_diff(path: str, old: str, new: str, context: int = 4) -> Optional[tu
is quadratic, for example on repetitive lines) sees only the changed middle, at most
``DIFF_MAX_LINES`` lines on each side.
"""
- a, b = old.splitlines(keepends=True), new.splitlines(keepends=True)
+ a, b = _lines_with_breaks(old), _lines_with_breaks(new)
shortest = min(len(a), len(b))
prefix = 0
while prefix < shortest and a[prefix] == b[prefix]:
@@ -350,15 +350,30 @@ def display_diff(path: str, old: str, new: str, context: int = 4) -> Optional[tu
return diff, first_changed_line, _unified_patch(path, a, b, opcodes, context)
+def _lines_with_breaks(text: str) -> list[str]:
+ """Lines split at "\n" only, each keeping its "\n", as jsdiff's ``diffLines`` sees them.
+
+ ``str.splitlines`` also breaks at \f, \v, \x1c-\x1e, \x85, U+2028 and U+2029, which are not line
+ breaks for ``read``, ``edit``, or a patch.
+ """
+ lines = [line + "\n" for line in text.split("\n")]
+ lines[-1] = lines[-1][:-1]
+ return lines if lines[-1] else lines[:-1]
+
+
def _unified_range(start: int, stop: int) -> str:
+ """jsdiff's hunk range: always ``start,count``, and an empty range starts at the line before."""
length = stop - start
- if length == 1:
- return str(start + 1)
return f"{start + 1 if length else start},{length}"
+def _patch_line(sign: str, line: str) -> str:
+ # Only a file's last line can lack "\n"; jsdiff marks it the way GNU diff does.
+ return sign + line if line.endswith("\n") else f"{sign}{line}\n\\ No newline at end of file\n"
+
+
def _unified_patch(path: str, a: list[str], b: list[str], opcodes: list, context: int) -> str:
- """``difflib.unified_diff``'s output, from the given opcodes."""
+ """Pi's ``generateUnifiedPatch`` (jsdiff ``createTwoFilesPatch`` with file headers only), from the opcodes."""
groups = list(_FixedOpcodes(a, b, opcodes).get_grouped_opcodes(context))
if not groups:
return ""
@@ -368,18 +383,23 @@ def _unified_patch(path: str, a: list[str], b: list[str], opcodes: list, context
out.append(f"@@ -{_unified_range(first[1], last[2])} +{_unified_range(first[3], last[4])} @@\n")
for tag, i1, i2, j1, j2 in group:
if tag == "equal":
- out.extend(" " + line for line in a[i1:i2])
+ out.extend(_patch_line(" ", line) for line in a[i1:i2])
continue
- out.extend("-" + line for line in a[i1:i2])
- out.extend("+" + line for line in b[j1:j2])
+ out.extend(_patch_line("-", line) for line in a[i1:i2])
+ out.extend(_patch_line("+", line) for line in b[j1:j2])
return "".join(out)
+def _split_count(lines: list[str]) -> int:
+ return len(lines) + (0 if lines and not lines[-1].endswith("\n") else 1)
+
+
def _render_diff(a: list[str], b: list[str], opcodes: list, context: int) -> tuple[str, Optional[int]]:
"""Pi's display diff: numbered ``+``/``-``/context lines and the first changed line of the new file."""
- a = [line.rstrip("\r\n") for line in a]
- b = [line.rstrip("\r\n") for line in b]
- width = len(str(max(len(a), len(b)) + 1))
+ # Pi's width: the digits of the larger ``split("\n")`` count, which has one more entry after a final "\n".
+ width = len(str(max(_split_count(a), _split_count(b))))
+ a = [line[:-1] if line.endswith("\n") else line for line in a]
+ b = [line[:-1] if line.endswith("\n") else line for line in b]
parts: list[tuple[str, list[str]]] = []
for tag, i1, i2, j1, j2 in opcodes:
if tag == "equal":
diff --git a/core/agent_core/tools/paths.py b/core/agent_core/tools/paths.py
index 9d330a8fa..1af590c4b 100644
--- a/core/agent_core/tools/paths.py
+++ b/core/agent_core/tools/paths.py
@@ -14,11 +14,13 @@ import re
import stat
import unicodedata
from contextlib import asynccontextmanager
-from typing import AsyncIterator, Literal
+from typing import Any, AsyncIterator, Callable, Literal, TypeVar
from urllib.parse import unquote, urlparse
from core.agent_core.tools.args import ToolInputError
+T = TypeVar("T")
+
_UNICODE_SPACES = re.compile("[\u00a0\u2000-\u200a\u202f\u205f\u3000]")
_NARROW_NO_BREAK_SPACE = "\u202f"
_AM_PM = re.compile(r" (AM|PM)\.", re.IGNORECASE)
@@ -196,3 +198,24 @@ async def file_mutation_lock(path: str) -> AsyncIterator[None]:
entry.users -= 1
if entry.users == 0:
del _locks[key]
+
+
+async def to_thread_joined(func: Callable[..., T], /, *args: Any) -> T:
+ """``asyncio.to_thread`` for work inside ``file_mutation_lock``: a cancel waits for the thread.
+
+ Cancelling an await does not stop its worker thread, so without the wait a write could land after
+ the lock was released and overwrite the next writer's result. The cancel is re-raised once the
+ thread is done, however often the caller is cancelled and even when the thread raised.
+ """
+ worker = asyncio.ensure_future(asyncio.to_thread(func, *args))
+ try:
+ return await asyncio.shield(worker)
+ except asyncio.CancelledError:
+ while not worker.done():
+ try:
+ await asyncio.wait({worker})
+ except asyncio.CancelledError:
+ pass
+ if not worker.cancelled():
+ worker.exception() # retrieved; the caller's cancel is what propagates
+ raise
diff --git a/core/agent_core/tools/write.py b/core/agent_core/tools/write.py
index 2c530cc5e..4fcc618e0 100644
--- a/core/agent_core/tools/write.py
+++ b/core/agent_core/tools/write.py
@@ -22,6 +22,7 @@ from core.agent_core.tools.paths import (
os_reason,
resolve_to_cwd,
target_kind,
+ to_thread_joined,
)
from core.agent_core.tools.text import model_text
@@ -65,12 +66,12 @@ class WriteTool:
if ctx.cancel.cancelled:
return error_result("Operation aborted")
try:
- refusal = await asyncio.to_thread(_prepare_target, absolute)
+ refusal = await to_thread_joined(_prepare_target, absolute)
if refusal:
return error_result(f"Cannot write {path}: {refusal}.")
if ctx.cancel.cancelled:
return error_result("Operation aborted")
- await asyncio.to_thread(write_text, absolute, content)
+ await to_thread_joined(write_text, absolute, content)
except OSError as exc:
return error_result(f"Cannot write {path}: {os_reason(exc)}.")
return text_result(f"Successfully wrote to {path}")
diff --git a/tests/agent_core/tools/test_bash.py b/tests/agent_core/tools/test_bash.py
index 8570882b8..1b82d999e 100644
--- a/tests/agent_core/tools/test_bash.py
+++ b/tests/agent_core/tools/test_bash.py
@@ -10,6 +10,7 @@ import time
import psutil
import pytest
+from core.agent_core.agent.jobs import TrackingJobHost
from core.agent_core.cancel import CancelToken
from core.agent_core.tools.bash import BashTool, settle_bash_call
import core.agent_core.tools.jobs as jobs_module
@@ -182,6 +183,24 @@ async def test_abort_kills_the_command(tmp_path, make_ctx):
assert _process_gone(int((tmp_path / "sh.pid").read_text()))
+ ("arguments", "abort", "expected"),
+ [
+ ({"command": "echo started; sleep 30", "timeout": 1.5}, False, "started\n\n\nCommand timed out after 1.5 seconds"),
+ ({"command": "echo started; sleep 30"}, True, "started\n\n\nCommand aborted"),
+ ],
+ ids=["timeout", "abort"],
+)
+async def test_stops_are_reported_through_the_agents_job_host(tmp_path, make_ctx, arguments, abort, expected):
+ """bash gets the loop's TrackingJobHost (Agent.jobs); reasoned kills and stop reasons pass through it."""
+ cancel = CancelToken()
+ ctx = make_ctx(cancel=cancel, on_progress=lambda tail: abort and cancel.cancel())
+
+ result = await BashTool(TrackingJobHost(_host(tmp_path))).execute(arguments, ctx)
+
+ assert (result.is_error, result_text(result)) == (True, expected)
+
+
async def test_the_foreground_window_hands_over_and_the_command_runs_once(tmp_path, make_ctx):
watches = Watches()
host = _host(tmp_path, watches)
diff --git a/tests/agent_core/tools/test_write_edit.py b/tests/agent_core/tools/test_write_edit.py
index 6eda3466d..fdc2a84f5 100644
--- a/tests/agent_core/tools/test_write_edit.py
+++ b/tests/agent_core/tools/test_write_edit.py
@@ -2,11 +2,13 @@
from __future__ import annotations
+import asyncio
import errno
import os
import random
import re
import stat
+import threading
import time
import pytest
@@ -174,6 +176,63 @@ async def test_replacement_contents_stay_private_until_the_rename(tmp_path, make
assert secret.read_text() == "new secret\n"
+_WRITE_FIRST = {"path": "f.txt", "content": "first\n"}
+
+
+ ("first", "cancels", "worker_fails"),
+ [
+ (_WRITE_FIRST, 1, False),
+ ({"path": "f.txt", "edits": [{"oldText": "original", "newText": "first"}]}, 1, False),
+ (_WRITE_FIRST, 2, False),
+ (_WRITE_FIRST, 1, True),
+ ],
+ ids=["write", "edit", "cancelled-twice", "worker-fails"],
+)
+async def test_a_cancelled_write_lands_before_the_next_writer_takes_the_lock(
+ tmp_path, make_ctx, monkeypatch, first, cancels, worker_fails
+):
+ """Cancelling an await does not stop its worker thread; the lock is held until the worker is done."""
+ (tmp_path / "f.txt").write_text("original\n")
+ entered, release, renamed = threading.Event(), threading.Event(), threading.Event()
+ real_fsync, real_replace, calls, renames = write_module.os.fsync, write_module.os.replace, [], []
+ expected_renames = 1 if worker_fails else 2
+
+ def first_fsync_blocks(fd):
+ calls.append(fd)
+ if len(calls) == 1:
+ entered.set()
+ release.wait(10)
+ if worker_fails:
+ raise OSError(errno.ENOSPC, "No space left on device")
+ real_fsync(fd)
+
+ def counted_replace(source, target):
+ real_replace(source, target)
+ renames.append(target)
+ if len(renames) == expected_renames:
+ renamed.set()
+
+ monkeypatch.setattr(write_module.os, "fsync", first_fsync_blocks)
+ monkeypatch.setattr(write_module.os, "replace", counted_replace)
+ tool = WriteTool() if "content" in first else EditTool()
+ cancelled = asyncio.ensure_future(tool.execute(first, make_ctx()))
+ await asyncio.to_thread(entered.wait, 10)
+ for _ in range(cancels):
+ cancelled.cancel()
+ await asyncio.sleep(0.05)
+ later = asyncio.ensure_future(WriteTool().execute({"path": "f.txt", "content": "second\n"}, make_ctx()))
+ await asyncio.sleep(0.3) # time for the later write to get through a lock released too early
+ release.set()
+ outcomes = await asyncio.gather(cancelled, later, return_exceptions=True)
+ assert await asyncio.to_thread(renamed.wait, 10)
+
+ # The cancel propagates even when the worker failed; it is never turned into a result.
+ assert isinstance(outcomes[0], asyncio.CancelledError)
+ assert not outcomes[1].is_error, result_text(outcomes[1])
+ assert (tmp_path / "f.txt").read_text() == "second\n"
+
+
async def test_new_files_keep_the_umask_default(tmp_path, make_ctx):
umask = os.umask(0)
os.umask(umask)
@@ -206,6 +265,62 @@ async def test_the_display_diff_is_bounded_by_lines_not_only_bytes(tmp_path, mak
assert "@@ -4997,9 +4997,9 @@" in result.details["patch"]
+# Expected values are the output of Pi's generateDiffString and generateUnifiedPatch (jsdiff 8.0.4).
+ ("original", "edit", "diff", "first_changed_line", "patch"),
+ [
+ (
+ "x\na",
+ {"oldText": "a", "newText": "b"},
+ " 1 x\n-2 a\n+2 b",
+ 2,
+ "@@ -1,2 +1,2 @@\n x\n-a\n\\ No newline at end of file\n+b\n\\ No newline at end of file\n",
+ ),
+ (
+ "a\n",
+ {"oldText": "a\n", "newText": "a"},
+ "-1 a\n+1 a",
+ 1,
+ "@@ -1,1 +1,1 @@\n-a\n+a\n\\ No newline at end of file\n",
+ ),
+ (
+ # A form feed is not a line break.
+ "a\fb\nc\n",
+ {"oldText": "c", "newText": "C"},
+ " 1 a\fb\n-2 c\n+2 C",
+ 2,
+ "@@ -1,2 +1,2 @@\n a\fb\n-c\n+C\n",
+ ),
+ (
+ "one\n",
+ {"oldText": "one\n", "newText": "one\ntwo\n"},
+ " 1 one\n+2 two",
+ 2,
+ "@@ -1,1 +1,2 @@\n one\n+two\n",
+ ),
+ (
+ "".join(f"{i}\n" for i in range(1, 9)) + "9",
+ {"oldText": "9", "newText": "nine"},
+ " ...\n 5 5\n 6 6\n 7 7\n 8 8\n-9 9\n+9 nine",
+ 9,
+ "@@ -5,5 +5,5 @@\n 5\n 6\n 7\n 8\n-9\n\\ No newline at end of file\n+nine\n\\ No newline at end of file\n",
+ ),
+ ],
+ ids=["no-final-newline", "final-newline-removed", "form-feed", "insertion", "width"],
+)
+async def test_the_display_diff_and_patch_are_pis(tmp_path, make_ctx, original, edit, diff, first_changed_line, patch):
+ (tmp_path / "f.txt").write_bytes(original.encode())
+
+ result = await _edit(make_ctx, "f.txt", edit)
+
+ assert not result.is_error, result_text(result)
+ assert result.details == {
+ "diff": diff,
+ "patch": "--- f.txt\n+++ f.txt\n" + patch,
+ "first_changed_line": first_changed_line,
+ }
+
+
async def test_a_large_changed_middle_skips_the_display_diff(tmp_path, make_ctx):
(tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(3000)))