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): one open per model-named file, private temps, a binding handover

cyh69eb278f0320
diff --git a/core/agent_core/tools/bash.py b/core/agent_core/tools/bash.py
index 86e781954..df2b34f59 100644
--- a/core/agent_core/tools/bash.py
+++ b/core/agent_core/tools/bash.py
@@ -27,7 +27,7 @@ from core.agent_core.tools.args import (
text_result,
)
from core.agent_core.tools.base import MAX_BYTES, MAX_LINES, JobHost, JobStatus, ToolContext, ToolResult, ToolSpec
-from core.agent_core.tools.jobs import STOP_ABORTED, STOP_TIMEOUT, JobStartError, LocalJobHost
+from core.agent_core.tools.jobs import STOP_ABORTED, STOP_TIMEOUT, JobHandOverUnavailable, JobStartError, LocalJobHost
from core.agent_core.tools.paths import os_reason
from core.agent_core.tools.output import JobOutput
@@ -231,18 +231,34 @@ class BashTool:
await self._jobs.kill(job_id, reason=STOP_TIMEOUT)
return stopped_result(output, _timed_out_line(timeout))
if hand_over:
- try:
- return handover_result(output, await self._jobs.hand_over(job_id))
- except Exception as exc: # no Watch here: the command stays in the foreground
- logger.info("Job %s stays in the foreground: %s", job_id, exc)
- handover_error = exc
- continue
+ watch_id, handover_error = await self._hand_over(job_id)
+ if watch_id is not None:
+ return handover_result(output, watch_id)
+ continue
if ctx.on_progress is not None and output.poll():
tail = output.snapshot().content
if tail != last_progress:
last_progress = tail
ctx.on_progress(tail)
+ async def _hand_over(self, job_id: str) -> tuple[Optional[str], Optional[Exception]]:
+ """``(watch_id, None)``, or ``(None, error)`` when no Watch owns the job and it stays in the foreground.
+
+ ``hand_over`` raising means no Watch owns the job, and adopt-or-create is idempotent, so one
+ failed attempt is tried once more; ``JobHandOverUnavailable`` means there is no Watch to try.
+ """
+ error: Optional[Exception] = None
+ for _ in range(2):
+ try:
+ return await self._jobs.hand_over(job_id), None
+ except JobHandOverUnavailable as exc:
+ error = exc
+ break
+ except Exception as exc:
+ error = exc
+ logger.info("Job %s stays in the foreground: %s", job_id, error)
+ return None, error
+
async def _wait_or_cancel(self, job_id: str, seconds: float, cancel: CancelToken) -> Optional[JobStatus]:
"""The job's status after at most ``seconds``, or ``None`` as soon as ``cancel`` fires."""
if cancel.cancelled:
diff --git a/core/agent_core/tools/edit.py b/core/agent_core/tools/edit.py
index d5f015b2a..2f9525be8 100644
--- a/core/agent_core/tools/edit.py
+++ b/core/agent_core/tools/edit.py
@@ -11,18 +11,24 @@ from __future__ import annotations
import asyncio
import json
import os
-import re
from typing import Any, Mapping
from core.agent_core.tools.args import ToolInputError, error_result, str_arg, text_result
from core.agent_core.tools.base import ToolContext, ToolResult, ToolSpec
from core.agent_core.tools.edit_diff import Edit, EditError, apply_edits, generate_diff_string, generate_unified_patch
-from core.agent_core.tools.paths import errno_name, file_mutation_lock, resolve_to_cwd, target_kind
-from core.agent_core.tools.text import decode_file, encode_file
+from core.agent_core.tools.paths import (
+ NotRegularFile,
+ errno_name,
+ file_mutation_lock,
+ open_regular,
+ read_at_most,
+ resolve_to_cwd,
+ target_kind,
+)
+from core.agent_core.tools.text import decode_file, encode_file, model_text
from core.agent_core.tools.truncate import format_size
from core.agent_core.tools.write import write_bytes
-_SURROGATE = re.compile("[\ud800-\udfff]")
#: Avibe: larger files are refused rather than loaded whole; the display diff stops at a smaller size.
MAX_EDIT_BYTES = 10 * 1024 * 1024
MAX_DIFF_BYTES = 1024 * 1024
@@ -114,19 +120,35 @@ def _edits_arg(arguments: Mapping[str, Any]) -> list[Edit]:
if not isinstance(replace_all, bool):
raise ToolInputError(f"edits[{index}].replaceAll must be a boolean")
# A lone surrogate (possible from JSON) cannot be written; it becomes U+FFFD.
- parsed.append(Edit(_SURROGATE.sub("\ufffd", old_text), _SURROGATE.sub("\ufffd", new_text), replace_all))
+ parsed.append(Edit(model_text(old_text), model_text(new_text), replace_all))
return parsed
-def _plan_edits(absolute: str, edits: list[Edit], path: str) -> tuple[bytes, str, str]:
- """Read the file and compute its new bytes, with the LF views before and after for the diff.
+class _TooLarge(Exception):
+ def __init__(self, size: int) -> None:
+ super().__init__(size)
+ self.size = size
+
+
+def _plan_edits(absolute: str, edits: list[Edit], path: str) -> tuple[int, bytes, str, str]:
+ """Read the file and compute its new bytes: ``(size, data, view_before, view_after)``.
- ``surrogateescape`` carries bytes that are not UTF-8 through unchanged; only replaced spans change.
+ One descriptor gives the size and the contents, read up to the limit, so a file that grew after an
+ earlier check by path cannot get past it. ``surrogateescape`` carries bytes that are not UTF-8
+ through unchanged; only replaced spans change.
"""
- with open(absolute, "rb") as handle:
- text = decode_file(handle.read())
- new_text, before, after = apply_edits(text, edits, path)
- return encode_file(new_text), before, after
+ fd = open_regular(absolute)
+ try:
+ size = os.fstat(fd).st_size
+ if size > MAX_EDIT_BYTES:
+ raise _TooLarge(size)
+ raw = read_at_most(fd, MAX_EDIT_BYTES)
+ finally:
+ os.close(fd)
+ if len(raw) > MAX_EDIT_BYTES:
+ raise _TooLarge(max(size, len(raw)))
+ new_text, before, after = apply_edits(decode_file(raw), edits, path)
+ return len(raw), encode_file(new_text), before, after
class EditTool:
@@ -161,14 +183,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 = os.path.getsize(absolute)
- if size > MAX_EDIT_BYTES:
- # The whole file and several copies would sit in the process every Session shares.
- return error_result(
- f"File {path} is {format_size(size)}, over the {format_size(MAX_EDIT_BYTES)} edit limit. "
- "Use bash (for example sed or a short script) to change files this large."
- )
- data, base, new_content = await asyncio.to_thread(_plan_edits, absolute, edits, path)
+ size, data, base, new_content = await asyncio.to_thread(_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 "
@@ -178,8 +193,17 @@ class EditTool:
if ctx.cancel.cancelled:
return error_result("Operation aborted")
await asyncio.to_thread(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(
+ f"File {path} is {format_size(exc.size)}, over the {format_size(MAX_EDIT_BYTES)} edit limit. "
+ "Use bash (for example sed or a short script) to change files this large."
+ )
except EditError as exc:
return error_result(str(exc))
+ except NotRegularFile as exc:
+ detail = "Error code: EISDIR." if exc.kind == "directory" else "It is not a regular file."
+ return error_result(f"Could not edit file: {path}. {detail}")
except OSError as exc:
return error_result(f"Could not edit file: {path}. Error code: {errno_name(exc)}.")
diff --git a/core/agent_core/tools/jobs.py b/core/agent_core/tools/jobs.py
index 021f2dcc0..59f2b6ed5 100644
--- a/core/agent_core/tools/jobs.py
+++ b/core/agent_core/tools/jobs.py
@@ -67,7 +67,8 @@ from core.process_isolation import (
logger = logging.getLogger(__name__)
#: Adopt-or-create the once Watch (target kind ``job``) for the job's ``meta.json``; returns the Watch id.
-#: Keyed by ``job_id``, so calling it again after a crash returns the same Watch (J6).
+#: Keyed by ``job_id``, so calling it again after a crash returns the same Watch (J6). The adapter promises
+#: that raising means no Watch owns the job: adopt-or-create is atomic. Once it returns, the Watch owns it.
HandOver = Callable[[Mapping[str, Any]], Awaitable[str]]
#: Whether the tool call that owns a job has a durable ``tool_result`` and its Watch, if any, has settled (J5).
Settled = Callable[[Mapping[str, Any]], bool]
@@ -420,16 +421,26 @@ class LocalJobHost:
logger.warning("Job %s process group %s survived termination", job_id, identity.pid)
async def hand_over(self, job_id: str) -> str:
- """Give the job to its Watch: adopt-or-create through ``on_hand_over``, then record the id (J6)."""
+ """Give the job to its Watch: adopt-or-create through ``on_hand_over``, then record the id (J6).
+
+ Raising means no Watch owns the job: it never raises after ``on_hand_over`` returned.
+ """
meta = self.meta(job_id)
if meta.get("watch_id"):
return meta["watch_id"]
if self._on_hand_over is None:
raise JobHandOverUnavailable("No Watch is available to take over the command.")
watch_id = await self._on_hand_over(meta)
- meta = self.meta(job_id)
- meta["watch_id"] = watch_id
- self._write_meta(job_id, meta)
+ # The Watch owns the job from here on, so nothing below may undo the handover: the recorded id
+ # is a cache, and adopt-or-create finds the Watch by job id without it.
+ try:
+ meta = self.meta(job_id)
+ meta["watch_id"] = watch_id
+ self._write_meta(job_id, meta)
+ except (OSError, KeyError, ValueError):
+ logger.warning(
+ "Job %s is Watch %s, but its meta.json could not record that", job_id, watch_id, exc_info=True
+ )
return watch_id
# --- deadline (J3) and reporting --------------------------------------
diff --git a/core/agent_core/tools/paths.py b/core/agent_core/tools/paths.py
index 8a6f0a744..9d330a8fa 100644
--- a/core/agent_core/tools/paths.py
+++ b/core/agent_core/tools/paths.py
@@ -118,6 +118,44 @@ def target_kind(path: str) -> FileKind:
return "directory" if stat.S_ISDIR(mode) else "other"
+class NotRegularFile(OSError):
+ """The descriptor names a directory or a special file; ``kind`` says which."""
+
+ def __init__(self, kind: FileKind) -> None:
+ super().__init__(KIND_REASON[kind])
+ self.kind = kind
+
+
+def open_regular(path: str, flags: int = os.O_RDONLY) -> int:
+ """Open a model-named file once and return the descriptor, which is a regular file.
+
+ ``O_NONBLOCK`` keeps a FIFO swapped in after an earlier check from blocking the open, and the
+ kind is taken from the descriptor (``fstat``), so sizes and contents come from the same file.
+ Raises ``NotRegularFile`` or the ``OSError`` of the open.
+ """
+ fd = os.open(path, flags | os.O_NONBLOCK | getattr(os, "O_CLOEXEC", 0))
+ try:
+ mode = os.fstat(fd).st_mode
+ if not stat.S_ISREG(mode):
+ raise NotRegularFile("directory" if stat.S_ISDIR(mode) else "other")
+ except BaseException:
+ os.close(fd)
+ raise
+ return fd
+
+
+def read_at_most(fd: int, limit: int) -> bytes:
+ """Up to ``limit + 1`` bytes from ``fd``, so a caller can tell the file is over ``limit``."""
+ chunks, remaining = [], limit + 1
+ while remaining > 0:
+ chunk = os.read(fd, min(remaining, 1024 * 1024))
+ if not chunk:
+ break
+ chunks.append(chunk)
+ remaining -= len(chunk)
+ return b"".join(chunks)
+
+
def os_reason(error: OSError) -> str:
"""``No such file or directory`` → ``no such file or directory``, for ``Cannot … {path}: {reason}.``"""
text = error.strerror or str(error) or type(error).__name__
diff --git a/core/agent_core/tools/read.py b/core/agent_core/tools/read.py
index 5d9979488..6eab1c9ae 100644
--- a/core/agent_core/tools/read.py
+++ b/core/agent_core/tools/read.py
@@ -16,7 +16,8 @@ import inspect
import os
import shlex
import struct
-from typing import Any, Awaitable, Callable, Mapping, Optional, Union
+from dataclasses import dataclass
+from typing import Any, Awaitable, BinaryIO, Callable, Mapping, Optional, Union
from core.agent_core.messages import IMAGE_MIME_TYPES, ImageBlock, TextBlock
from core.agent_core.tools.args import (
@@ -27,7 +28,14 @@ from core.agent_core.tools.args import (
text_result,
)
from core.agent_core.tools.base import MAX_BYTES, MAX_LINES, ToolContext, ToolResult, ToolSpec
-from core.agent_core.tools.paths import KIND_REASON, os_reason, resolve_read_path, target_kind
+from core.agent_core.tools.paths import (
+ KIND_REASON,
+ NotRegularFile,
+ open_regular,
+ os_reason,
+ resolve_read_path,
+ target_kind,
+)
from core.agent_core.tools.text import decode_file, shown
from core.agent_core.tools.truncate import format_size, truncate_head
@@ -83,48 +91,42 @@ class ReadTool:
refusal = await asyncio.to_thread(_refusal, absolute)
if refusal:
return error_result(f"Cannot read {path}: {refusal}.")
- mime_type = await asyncio.to_thread(_sniff_image, absolute)
- if mime_type is not None:
- return await self._read_image(absolute, mime_type)
# Off the event loop, which every Session and surface shares; the scan stops on cancel.
- return await asyncio.to_thread(_read_text, absolute, path, offset, limit, lambda: ctx.cancel.cancelled)
+ loaded = await asyncio.to_thread(_load, absolute, path, offset, limit, lambda: ctx.cancel.cancelled)
+ if isinstance(loaded, _Image):
+ return await self._attach(absolute, loaded)
+ return loaded
except _Aborted:
return error_result("Operation aborted")
+ except NotRegularFile as exc:
+ return error_result(f"Cannot read {path}: {KIND_REASON[exc.kind]}.")
except OSError as exc:
return error_result(f"Cannot read {path}: {os_reason(exc)}.")
except ToolInputError as exc:
return error_result(str(exc))
- async def _read_image(self, absolute: str, mime_type: str) -> ToolResult:
- header = f"Read image file [{mime_type}]"
- details = {"path": absolute, "mime_type": mime_type}
- if mime_type not in IMAGE_MIME_TYPES:
- # Pi converts other formats to PNG; without an image library Avibe cannot.
- return text_result(
- f"{header}\n[Image omitted: could not be converted to a supported inline image format.]",
- details=details,
- )
- size = os.path.getsize(absolute)
- if size > MAX_INLINE_IMAGE_BYTES:
- # Avibe: no resizing in v1.
- return text_result(
- f"{header}\n[Image omitted: the file is {format_size(size)}, over the "
- f"{format_size(MAX_INLINE_IMAGE_BYTES)} inline image limit. Images are not resized.]",
- details={**details, "bytes": size},
- )
+ async def _attach(self, absolute: str, image: "_Image") -> ToolResult:
+ header = f"Read image file [{image.mime_type}]"
+ details = {"path": absolute, "mime_type": image.mime_type, "bytes": image.size}
if self._image_sink is None:
return text_result(f"{header}\n[Image omitted: image attachments are not available.]", details=details)
- data = await asyncio.to_thread(_read_bytes, absolute)
name = os.path.basename(absolute)
- token = self._image_sink(data, mime_type, name)
+ token = self._image_sink(image.data, image.mime_type, name)
if inspect.isawaitable(token):
token = await token
return ToolResult(
- content=(TextBlock(text=header), ImageBlock(mime_type=mime_type, media_token=token, name=name)),
- details={**details, "bytes": len(data)},
+ content=(TextBlock(text=header), ImageBlock(mime_type=image.mime_type, media_token=token, name=name)),
+ details=details,
)
+@dataclass(frozen=True)
+class _Image:
+ mime_type: str
+ data: bytes
+ size: int
+
+
class _Aborted(Exception):
pass
@@ -142,13 +144,42 @@ def _refusal(absolute: str) -> Optional[str]:
return None
-def _sniff_image(path: str) -> Optional[str]:
+def _load(
+ absolute: str, path: str, offset: Optional[int], limit: Optional[int], cancelled: Callable[[], bool]
+) -> Union[ToolResult, _Image]:
+ """Everything read takes from the file, through one descriptor: the kind, the image type, the size,
+ and the bytes. A file swapped or grown after an earlier check by path cannot get past its limits."""
+ with os.fdopen(open_regular(absolute), "rb") as handle:
+ mime_type = _sniff_image(handle)
+ if mime_type is None:
+ return _read_text(handle, absolute, path, offset, limit, cancelled)
+ header = f"Read image file [{mime_type}]"
+ details = {"path": absolute, "mime_type": mime_type}
+ if mime_type not in IMAGE_MIME_TYPES:
+ # Pi converts other formats to PNG; without an image library Avibe cannot.
+ return text_result(
+ f"{header}\n[Image omitted: could not be converted to a supported inline image format.]",
+ details=details,
+ )
+ handle.seek(0)
+ data = handle.read(MAX_INLINE_IMAGE_BYTES + 1)
+ size = max(os.fstat(handle.fileno()).st_size, len(data))
+ if size > MAX_INLINE_IMAGE_BYTES:
+ # Avibe: no resizing in v1.
+ return text_result(
+ f"{header}\n[Image omitted: the file is {format_size(size)}, over the "
+ f"{format_size(MAX_INLINE_IMAGE_BYTES)} inline image limit. Images are not resized.]",
+ details={**details, "bytes": size},
+ )
+ return _Image(mime_type, data, len(data))
+
+
+def _sniff_image(handle: BinaryIO) -> Optional[str]:
"""The image type to attach, or ``None`` for text. A PNG or JPEG Pi cannot send (animated PNG,
JPEG-LS) keeps its family, so it is omitted rather than decoded as text."""
- with open(path, "rb") as handle:
- head = handle.read(_IMAGE_SNIFF_BYTES)
+ head = handle.read(_IMAGE_SNIFF_BYTES)
supported = detect_supported_image_mime_type(head)
- if supported == "image/png" and _png_file_is_animated(path):
+ if supported == "image/png" and _png_file_is_animated(handle):
# acTL may sit past the sniffed prefix, behind large ancillary chunks.
return "image/apng"
if supported is not None:
@@ -160,17 +191,17 @@ def _sniff_image(path: str) -> Optional[str]:
return None
-def _read_bytes(path: str) -> bytes:
- with open(path, "rb") as handle:
- return handle.read()
-
-
def _read_text(
- absolute: str, path: str, offset: Optional[int], limit: Optional[int], cancelled: Callable[[], bool]
+ handle: BinaryIO,
+ absolute: str,
+ path: str,
+ offset: Optional[int],
+ limit: Optional[int],
+ cancelled: Callable[[], bool],
) -> ToolResult:
start_line = offset - 1 if offset is not None else 0
stop_line = start_line + limit if limit is not None else None
- collected, total_lines, first_line_bytes = _scan_lines(absolute, start_line, stop_line, cancelled)
+ collected, total_lines, first_line_bytes = _scan_lines(handle, start_line, stop_line, cancelled)
if start_line >= total_lines:
raise ToolInputError(f"Offset {offset} is beyond end of file ({total_lines} lines total)")
@@ -205,7 +236,7 @@ def _read_text(
def _scan_lines(
- path: str, start: int, stop: Optional[int], cancelled: Callable[[], bool] = lambda: False
+ handle: BinaryIO, start: int, stop: Optional[int], cancelled: Callable[[], bool] = lambda: False
) -> tuple[list[str], int, int]:
"""Lines ``[start, stop)`` as Pi's ``text.split("\\n")`` gives them, without the ``\\r`` of a CRLF break,
the file's line count, and the raw size of line ``start``.
@@ -250,31 +281,31 @@ def _scan_lines(
first_line_bytes += (len(chunk) if newline == -1 else newline) - pos
index += chunk.count(b"\n", pos)
- with open(path, "rb") as handle:
- # A UTF-8 BOM is not shown, as edit does not match it, so text read shows can be edited.
- if handle.read(len(_BOM)) != _BOM:
- handle.seek(0)
- while chunk := handle.read(_READ_CHUNK_BYTES):
- if cancelled():
- raise _Aborted
- pos = 0
- while collecting:
- newline = chunk.find(b"\n", pos)
- end = len(chunk) if newline == -1 else newline
- if stop is not None and index >= stop:
- collecting = False
- continue
- if index == start:
- first_line_bytes += end - pos
- if index >= start:
- collect(chunk[pos:end], newline != -1)
- if newline == -1:
- pos = len(chunk)
- break
- index += 1
- pos = newline + 1
- else:
- count_rest(chunk, pos)
+ handle.seek(0)
+ # A UTF-8 BOM is not shown, as edit does not match it, so text read shows can be edited.
+ if handle.read(len(_BOM)) != _BOM:
+ handle.seek(0)
+ while chunk := handle.read(_READ_CHUNK_BYTES):
+ if cancelled():
+ raise _Aborted
+ pos = 0
+ while collecting:
+ newline = chunk.find(b"\n", pos)
+ end = len(chunk) if newline == -1 else newline
+ if stop is not None and index >= stop:
+ collecting = False
+ continue
+ if index == start:
+ first_line_bytes += end - pos
+ if index >= start:
+ collect(chunk[pos:end], newline != -1)
+ if newline == -1:
+ pos = len(chunk)
+ break
+ index += 1
+ pos = newline + 1
+ else:
+ count_rest(chunk, pos)
if collecting and opened != index and index >= start:
# An empty file: its one line is empty.
lines.append(bytearray())
@@ -296,22 +327,21 @@ def detect_supported_image_mime_type(data: bytes) -> Optional[str]:
return None
-def _png_file_is_animated(path: str) -> bool:
+def _png_file_is_animated(handle: BinaryIO) -> bool:
"""Walk the chunk headers until IDAT or acTL, seeking over chunk bodies; bounded by the file size."""
- size = os.path.getsize(path)
- with open(path, "rb") as handle:
- offset = len(_PNG_SIGNATURE)
- while offset + 8 <= size:
- handle.seek(offset)
- header = handle.read(8)
- if len(header) < 8:
- return False
- length, kind = struct.unpack(">I", header[:4])[0], header[4:]
- if kind == b"acTL":
- return True
- if kind == b"IDAT":
- return False
- offset += 12 + length
+ size = os.fstat(handle.fileno()).st_size
+ offset = len(_PNG_SIGNATURE)
+ while offset + 8 <= size:
+ handle.seek(offset)
+ header = handle.read(8)
+ if len(header) < 8:
+ return False
+ length, kind = struct.unpack(">I", header[:4])[0], header[4:]
+ if kind == b"acTL":
+ return True
+ if kind == b"IDAT":
+ return False
+ offset += 12 + length
return False
diff --git a/core/agent_core/tools/text.py b/core/agent_core/tools/text.py
index ee0fb7a1c..495fb9f78 100644
--- a/core/agent_core/tools/text.py
+++ b/core/agent_core/tools/text.py
@@ -9,7 +9,10 @@ sequence; Avibe shows one per invalid byte.)
from __future__ import annotations
+import re
+
_SHOWN = {code: "\ufffd" for code in range(0xDC80, 0xDD00)}
+_LONE_SURROGATE = re.compile("[\ud800-\udfff]")
def decode_file(data: bytes) -> str:
@@ -24,3 +27,11 @@ def shown(text: str) -> str:
def encode_file(text: str) -> bytes:
return text.encode("utf-8", "surrogateescape")
+
+
+def model_text(text: str) -> str:
+ """Text from the model, writable as UTF-8: a lone surrogate (JSON allows one) becomes U+FFFD.
+
+ The one sanitizer for model-supplied text, used by write's content and edit's oldText/newText.
+ """
+ return _LONE_SURROGATE.sub("\ufffd", text)
diff --git a/core/agent_core/tools/write.py b/core/agent_core/tools/write.py
index f5cb049b3..2c530cc5e 100644
--- a/core/agent_core/tools/write.py
+++ b/core/agent_core/tools/write.py
@@ -15,7 +15,19 @@ from typing import Any, Mapping, Optional
from core.agent_core.tools.args import ToolInputError, error_result, str_arg, text_result
from core.agent_core.tools.base import ToolContext, ToolResult, ToolSpec
-from core.agent_core.tools.paths import KIND_REASON, file_mutation_lock, os_reason, resolve_to_cwd, target_kind
+from core.agent_core.tools.paths import (
+ KIND_REASON,
+ file_mutation_lock,
+ open_regular,
+ os_reason,
+ resolve_to_cwd,
+ target_kind,
+)
+from core.agent_core.tools.text import model_text
+
+# The process umask, read once: new files get 0o666 under it, as open() would give them.
+_UMASK = os.umask(0)
+os.umask(_UMASK)
WRITE_DESCRIPTION = (
"Write content to a file. Creates the file if it doesn't exist, overwrites if it does. Automatically creates "
@@ -86,16 +98,17 @@ def _prepare_target(absolute: str) -> Optional[str]:
def write_text(path: str, content: str) -> None:
- """UTF-8 without newline translation; a lone surrogate (possible from JSON) is replaced."""
- write_bytes(path, content.encode("utf-8", "replace"))
+ """UTF-8 without newline translation, after the one sanitizer for model text."""
+ write_bytes(path, model_text(content).encode("utf-8"))
def write_bytes(path: str, data: bytes) -> None:
"""Create or replace ``path`` only once ``data`` is completely on disk.
A failed write (a full disk, a quota) leaves the original, or its absence, as it was. The temp
- file sits beside the real target (through symlinks); it takes the existing file's mode, or the
- umask default for a new file, is fsynced, and replaces the target with ``os.replace``.
+ file sits beside the real target (through symlinks) and is private (0600) while it holds the new
+ contents; once they are fsynced it takes the existing file's mode, or the umask default for a new
+ file, and replaces the target with ``os.replace``.
"""
target = os.path.realpath(path)
try:
@@ -104,12 +117,13 @@ def write_bytes(path: str, data: bytes) -> None:
mode = None
tmp = os.path.join(os.path.dirname(target), f".{os.path.basename(target)}.{secrets.token_hex(4)}.tmp")
try:
- fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o666)
+ fd = os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
except PermissionError:
if mode is None:
raise
# An existing, writable file in a directory we cannot add to: only an in-place write is possible.
- with open(target, "wb") as handle:
+ with os.fdopen(open_regular(target, os.O_WRONLY), "wb") as handle:
+ handle.truncate(0)
handle.write(data)
return
try:
@@ -117,8 +131,7 @@ def write_bytes(path: str, data: bytes) -> None:
handle.write(data)
handle.flush()
os.fsync(handle.fileno())
- if mode is not None:
- os.chmod(tmp, mode)
+ os.fchmod(handle.fileno(), mode if mode is not None else 0o666 & ~_UMASK)
os.replace(tmp, target)
except BaseException:
with contextlib.suppress(FileNotFoundError):
diff --git a/tests/agent_core/tools/test_bash.py b/tests/agent_core/tools/test_bash.py
index c6ed71e6f..74068fda5 100644
--- a/tests/agent_core/tools/test_bash.py
+++ b/tests/agent_core/tools/test_bash.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import asyncio
+import errno
import os
import time
@@ -224,6 +225,42 @@ async def test_watch_true_keeps_the_result_of_a_command_that_already_ended(tmp_p
assert watches.by_job == {}
+async def test_a_recorded_watch_is_a_handover_even_if_caching_its_id_fails(tmp_path, make_ctx, monkeypatch):
+ """hand_over raising means no Watch owns the job; once the Watch exists, the handover stands."""
+ watches = Watches()
+ host = _host(tmp_path, watches)
+ real_write = LocalJobHost._write_meta
+
+ def write_meta(self, job_id, meta):
+ if meta.get("watch_id"):
+ raise OSError(errno.ENOSPC, "No space left on device")
+ real_write(self, job_id, meta)
+
+ monkeypatch.setattr(LocalJobHost, "_write_meta", write_meta)
+ result = await BashTool(host).execute({"command": "sleep 5", "watch": True}, make_ctx())
+
+ assert result_text(result).startswith("Command is still running and is now Watch wch_1.")
+ assert list(watches.by_job.values()) == ["wch_1"]
+ await host.kill(result.details["job_id"])
+
+
+async def test_a_failing_watch_is_tried_again_before_the_command_stays_in_the_foreground(tmp_path, make_ctx):
+ calls = []
+
+ async def failing(meta):
+ calls.append(meta["job_id"])
+ raise RuntimeError("watch store unavailable")
+
+ result = await BashTool(_host(tmp_path, failing)).execute(
+ {"command": "sleep 1; echo hi", "watch": True}, make_ctx()
+ )
+
+ assert len(calls) == 2 and len(set(calls)) == 1
+ assert result_text(result) == (
+ "hi\n\n\n[Watch unavailable (watch store unavailable); the command ran in the foreground.]"
+ )
+
+
async def test_without_a_watch_the_command_stays_in_the_foreground(tmp_path, make_ctx):
result = await BashTool(_host(tmp_path)).execute({"command": "sleep 1; echo hi", "watch": True}, make_ctx())
diff --git a/tests/agent_core/tools/test_read.py b/tests/agent_core/tools/test_read.py
index 514679250..2257ebdad 100644
--- a/tests/agent_core/tools/test_read.py
+++ b/tests/agent_core/tools/test_read.py
@@ -2,6 +2,7 @@
from __future__ import annotations
+import asyncio
import os
import random
import shlex
@@ -134,7 +135,8 @@ async def test_streaming_matches_reading_the_whole_file(tmp_path, monkeypatch):
start = rng.randint(0, 6)
stop = None if rng.random() < 0.5 else start + rng.randint(1, 6)
- lines, total, first_line_bytes = read_module._scan_lines(str(path), start, stop)
+ with open(path, "rb") as handle:
+ lines, total, first_line_bytes = read_module._scan_lines(handle, start, stop)
expected, expected_total = _whole_file_read(data, start, stop)
assert total == expected_total, data
@@ -226,3 +228,36 @@ async def test_images_that_cannot_be_sent_as_they_are_are_omitted(
assert stored == []
assert result.content == (TextBlock(text=expected.format(size=len(data))),)
+
+
+async def test_read_takes_the_image_size_from_the_file_it_reads(tmp_path, make_ctx, monkeypatch):
+ """The image grew after a check by path: the cap holds on the bytes actually read."""
+ (tmp_path / "pic.png").write_bytes(_png() + b"\0" * 64)
+ monkeypatch.setattr(read_module, "MAX_INLINE_IMAGE_BYTES", 40)
+ monkeypatch.setattr(read_module.os.path, "getsize", lambda path: 10) # what an earlier check by path saw
+ stored = []
+
+ result = await ReadTool(image_sink=lambda *args: stored.append(args) or "never").execute(
+ {"path": "pic.png"}, make_ctx()
+ )
+
+ assert stored == []
+ assert "[Image omitted: the file is" in result_text(result)
+
+
+async def test_a_fifo_swapped_in_after_the_check_does_not_block_read(tmp_path, make_ctx, monkeypatch):
+ os.mkfifo(tmp_path / "p")
+ monkeypatch.setattr(read_module, "target_kind", lambda path: "regular") # it was a file when checked
+ task = asyncio.ensure_future(ReadTool().execute({"path": "p"}, make_ctx()))
+ try:
+ result = await asyncio.wait_for(asyncio.shield(task), timeout=5)
+ except asyncio.TimeoutError:
+ while not task.done():
+ try:
+ os.close(os.open(tmp_path / "p", os.O_WRONLY | os.O_NONBLOCK))
+ except OSError:
+ pass
+ await asyncio.sleep(0.05)
+ pytest.fail("read blocked on a FIFO")
+
+ assert (result.is_error, result_text(result)) == (True, "Cannot read p: it is not a regular file.")
diff --git a/tests/agent_core/tools/test_write_edit.py b/tests/agent_core/tools/test_write_edit.py
index fca6a9e67..b2d15cf5c 100644
--- a/tests/agent_core/tools/test_write_edit.py
+++ b/tests/agent_core/tools/test_write_edit.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import errno
+import os
import random
import re
import stat
@@ -139,6 +140,57 @@ async def test_text_copied_from_read_edits_the_first_line_of_a_bom_file(tmp_path
assert (tmp_path / "f.txt").read_bytes() == b"\xef\xbb\xbfALPHA\r\nbeta\r\n"
+async def test_edit_takes_the_size_from_the_file_it_reads(tmp_path, make_ctx, monkeypatch):
+ """The file grew after a check by path: the limit holds on the descriptor actually read."""
+ (tmp_path / "f.txt").write_text("hello\n" + "x" * 2000)
+ monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)
+ monkeypatch.setattr(edit_module.os.path, "getsize", lambda path: 10) # what an earlier check by path saw
+
+ result = await _edit(make_ctx, "f.txt", {"oldText": "hello", "newText": "bye"})
+
+ assert result.is_error
+ assert result_text(result).startswith("File f.txt is 2.0KB, over the 1000B edit limit.")
+ assert (tmp_path / "f.txt").read_text().startswith("hello")
+
+
+async def test_replacement_contents_stay_private_until_the_rename(tmp_path, make_ctx, monkeypatch):
+ secret = tmp_path / "token"
+ secret.write_text("old\n")
+ secret.chmod(0o600)
+ seen = []
+ real_fsync = write_module.os.fsync
+
+ def watch(fd):
+ seen.append(stat.S_IMODE(os.fstat(fd).st_mode))
+ real_fsync(fd)
+
+ monkeypatch.setattr(write_module.os, "fsync", watch)
+ result = await WriteTool().execute({"path": "token", "content": "new secret\n"}, make_ctx())
+
+ assert not result.is_error, result_text(result)
+ assert seen and all(mode & 0o077 == 0 for mode in seen)
+ assert stat.S_IMODE(secret.stat().st_mode) == 0o600
+ assert secret.read_text() == "new secret\n"
+
+
+async def test_new_files_keep_the_umask_default(tmp_path, make_ctx):
+ umask = os.umask(0)
+ os.umask(umask)
+
+ await WriteTool().execute({"path": "fresh.txt", "content": "x"}, make_ctx())
+
+ assert stat.S_IMODE((tmp_path / "fresh.txt").stat().st_mode) == 0o666 & ~umask
+
+
+async def test_write_and_edit_sanitize_model_text_the_same_way(tmp_path, make_ctx):
+ await WriteTool().execute({"path": "w.txt", "content": "a\ud800b"}, make_ctx())
+ (tmp_path / "e.txt").write_text("a-b")
+ await _edit(make_ctx, "e.txt", {"oldText": "-", "newText": "\udfff"})
+
+ assert (tmp_path / "w.txt").read_bytes() == "a\ufffdb".encode()
+ assert (tmp_path / "e.txt").read_bytes() == "a\ufffdb".encode()
+
+
async def test_a_planned_result_over_the_limit_is_refused_and_its_diff_skipped(tmp_path, make_ctx, monkeypatch):
(tmp_path / "small.txt").write_text("seed\n")
monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)