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.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(agent-core): whole-PR audit of resource bounds and classify-then-use in the tools (#2346)
Call Whitebc4714d382f6
diff --git a/core/agent_core/tools/bash.py b/core/agent_core/tools/bash.pyindex f441e19d5..051a758f8 100644--- a/core/agent_core/tools/bash.py+++ b/core/agent_core/tools/bash.py@@ -27,9 +27,15 @@ 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, JobHandOverUnavailable, 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+from core.agent_core.tools.output import FINISH_BYTES, JobOutputlogger = logging.getLogger(__name__)@@ -80,9 +86,9 @@ def _timeout_arg(arguments: Mapping[str, Any]) -> Optional[float]:return timeout-def final_result(output: JobOutput, status: JobStatus, *, note: Optional[str] = None) -> ToolResult:+async def final_result(output: JobOutput, status: JobStatus, *, note: Optional[str] = None) -> ToolResult:"""The result of a job that is no longer running, as Pi formats a finished command."""- output.finish()+ await output.finish_off_loop()text, truncation = output.render("(no output)")if note:text = f"{text}\n\n{note}"@@ -94,9 +100,9 @@ def final_result(output: JobOutput, status: JobStatus, *, note: Optional[str] =return error_result(_append_status(text, "Command terminated without an exit code"), details=details)-def stopped_result(output: JobOutput, status_line: str) -> ToolResult:+async def stopped_result(output: JobOutput, status_line: str) -> ToolResult:"""The result of a job that was killed (timeout, abort): the output so far, then why it stopped."""- output.finish()+ await output.finish_off_loop()text, truncation = output.render("")return error_result(_append_status(text, status_line), details=output.details(truncation))@@ -114,9 +120,10 @@ def _stopped_line(reason: Optional[str], timeout_s: Optional[float]) -> Optionalreturn None-def handover_result(output: JobOutput, watch_id: str) -> ToolResult:+async def handover_result(output: JobOutput, watch_id: str) -> ToolResult:"""Avibe: the command keeps running as a Watch; the model sees the output so far and how to manage it."""- output.poll()+ # A one-shot render reads the whole retained log (head, omitted middle, tail), not one poll's worth.+ await output.poll_off_loop(FINISH_BYTES)text, truncation = output.render("(no output yet)")text = text.rstrip("\n")lines = [@@ -145,11 +152,11 @@ async def settle_bash_call(jobs: LocalJobHost, session_id: str, tool_call_id: stawait jobs.enforce_deadline(job_id)status = jobs.status(job_id)if status.state == "running":- return handover_result(JobOutput(jobs, job_id), await jobs.hand_over(job_id))+ return await handover_result(JobOutput(jobs, job_id), await jobs.hand_over(job_id))if status.state == "exited":- return final_result(JobOutput(jobs, job_id), status)+ return await final_result(JobOutput(jobs, job_id), status)line = _stopped_line(jobs.stop_reason(job_id), jobs.meta(job_id).get("timeout_s"))- return stopped_result(JobOutput(jobs, job_id), line) if line else None+ return await stopped_result(JobOutput(jobs, job_id), line) if line else Noneclass BashTool:@@ -181,8 +188,7 @@ class BashTool:return error_result("Command aborted")# The job host enforces the timeout (its wrapper decides timeout versus exit); bash only reports- # the recorded reason. This clock times the handover.- started = time.monotonic()+ # the recorded reason.try:job_id = await self._jobs.start(command,@@ -198,6 +204,8 @@ class BashTool:logger.warning("Could not start a bash job", exc_info=True)return error_result(f"Could not start the command: {os_reason(exc)}.")+ # The handover clock starts once the command may run, so the launch never counts against it.+ started = time.monotonic()output = JobOutput(self._jobs, job_id)# watch=true still lets a command that ends at once report its own result instead of becoming a Watch.handover_at = min(self._window_s, _WATCH_GRACE_S) if watch else self._window_s@@ -216,21 +224,22 @@ class BashTool:# Whoever stopped the job recorded why; the job host or its wrapper may have stopped it first.line = _stopped_line(self._jobs.stop_reason(job_id), timeout)if line:- return stopped_result(output, line)+ return await stopped_result(output, line)if status is not None and status.state != "running":note = Noneif watch and handover_error is not None:note = f"[Watch unavailable ({handover_error}); the command ran in the foreground.]"- return final_result(output, status, note=note)+ return await final_result(output, status, note=note)if ctx.cancel.cancelled:await self._jobs.kill(job_id, reason=STOP_ABORTED)- return stopped_result(output, "Command aborted")+ return await stopped_result(output, "Command aborted")if hand_over:watch_id, handover_error = await self._hand_over(job_id)if watch_id is not None:- return handover_result(output, watch_id)+ return await handover_result(output, watch_id)continue- if ctx.on_progress is not None and output.poll():+ # Off the event loop: following a flood of output costs CPU every Session would otherwise wait on.+ if ctx.on_progress is not None and await output.poll_off_loop():tail = output.snapshot().contentif tail != last_progress:last_progress = taildiff --git a/core/agent_core/tools/edit_diff.py b/core/agent_core/tools/edit_diff.pyindex 8051fe8db..c13b9fbb6 100644--- a/core/agent_core/tools/edit_diff.py+++ b/core/agent_core/tools/edit_diff.py@@ -59,7 +59,39 @@ def normalize_to_lf(text: str) -> str:def normalize_for_fuzzy_match(text: str) -> str:"""NFKC, no trailing whitespace per line, ASCII quotes and dashes, plain spaces."""- text = unicodedata.normalize("NFKC", text)+ return _after_nfkc(unicodedata.normalize("NFKC", text))+++def _bounded_fuzzy_view(view: str) -> Optional[str]:+ """``normalize_for_fuzzy_match(view)``, or ``None`` once NFKC would grow it by more than+ ``MAX_FUZZY_GROWTH_CHARS`` (one character can become eighteen, U+FDFA).++ Text already in NFKC is not normalized again. Otherwise it is normalized a chunk at a time, each+ chunk ending just before an ASCII character (NFKC never composes anything with a following ASCII+ character, so the pieces join to the whole), stopping at the budget. A run of more than+ ``2 * _NFKC_CHUNK_CHARS`` characters without an ASCII character that is not in NFKC stops it too.+ """+ if unicodedata.is_normalized("NFKC", view):+ return _after_nfkc(view)+ budget = len(view) + MAX_FUZZY_GROWTH_CHARS+ pieces: list[str] = []+ total = pos = 0+ while pos < len(view):+ boundary = _ASCII.search(view, pos + _NFKC_CHUNK_CHARS)+ end = boundary.start() if boundary else len(view)+ chunk = view[pos:end]+ if len(chunk) > 2 * _NFKC_CHUNK_CHARS and not unicodedata.is_normalized("NFKC", chunk):+ return None+ piece = unicodedata.normalize("NFKC", chunk)+ total += len(piece)+ if total > budget:+ return None+ pieces.append(piece)+ pos = end+ return _after_nfkc("".join(pieces))+++def _after_nfkc(text: str) -> str:# rstrip, not a "[...]+$" regex: the regex retries at every space of a run, which is quadratic.text = "\n".join(line.rstrip(_TRAILING_SPACE) for line in text.split("\n"))text = _SMART_SINGLE.sub("'", text)@@ -68,9 +100,17 @@ def normalize_for_fuzzy_match(text: str) -> str:return _SPECIAL_SPACES.sub(" ", text)-#: Avibe: replacements one ``replaceAll`` may make. Each costs a few hundred bytes of Python objects in+#: Avibe: replacements one edit call may make. Each costs a few hundred bytes of Python objects in#: the process every Session shares, so the budget is structural, not only the file's size.MAX_REPLACEMENTS = 10_000+#: Avibe: NFKC can make one character eighteen (U+FDFA), and Pi normalizes the whole file. Loose+#: matching stops once normalization has grown the file by this many characters.+MAX_FUZZY_GROWTH_CHARS = 1024 * 1024+_NFKC_CHUNK_CHARS = 64 * 1024+_ASCII = re.compile("[\x00-\x7f]")+#: Avibe: every edit scans the whole file (``str.count``/``find``, which hold the GIL), so the number of+#: edits times the file's length is bounded: about a quarter second of scanning.+MAX_EDIT_SCAN_CHARS = 256 * 1024 * 1024class ResultTooLarge(Exception):@@ -86,7 +126,7 @@ def _find_all(content: str, needle: str, limit: int) -> list[int]:return found-def _tier(content: str, normalized: Callable[[], str], old_text: str) -> tuple[bool, str, str, int]:+def _tier(content: str, normalized: Callable[[], Optional[str]], old_text: str) -> tuple[bool, str, str, int]:"""``(used_normalized, haystack, needle, count)`` for the first tier where the edit occurs.Counting is ``str.count`` (no objects per match), so a match budget holds before any are built.@@ -95,22 +135,30 @@ def _tier(content: str, normalized: Callable[[], str], old_text: str) -> tuple[bcount = content.count(old_text)if count:return False, content, old_text, count+ if old_text.count("\n") > content.count("\n"):+ # Normalization keeps every line break (NFKC never makes or removes "\n"), so text with more lines+ # than the file cannot match; and model text that long is never split into per-line objects.+ return True, "", "", 0+ haystack = normalized()+ if haystack is None:+ return True, "", "", 0fuzzy_old = normalize_for_fuzzy_match(old_text)if not fuzzy_old:return True, "", "", 0- haystack = normalized()return True, haystack, fuzzy_old, haystack.count(fuzzy_old)-def _not_found(path: str, index: int, total: int) -> EditError:+def _not_found(path: str, index: int, total: int, *, loose_unavailable: bool = False) -> EditError:+ # Avibe addition: say when the normalized tier could not be tried, rather than that it found nothing.+ end = ": the file is too large to match it loosely after Unicode normalization." if loose_unavailable else "."if total == 1:return EditError(f"Could not find the exact text in {path}. The old text must match exactly including all whitespace "- "and newlines."+ f"and newlines{end}")return EditError(f"Could not find edits[{index}] in {path}. The oldText must match exactly including all whitespace and "- "newlines."+ f"newlines{end}")@@ -126,14 +174,25 @@ def _duplicate(path: str, index: int, total: int, occurrences: int) -> EditError)-def _too_many(path: str, index: int, total: int, occurrences: int) -> EditError:- which = "the text" if total == 1 else f"edits[{index}]"+def _too_many(path: str, total: int, replacements: int) -> EditError:+ if total == 1:+ return EditError(+ f"Found {replacements} occurrences of the text in {path}, over the {MAX_REPLACEMENTS} replaceAll limit. "+ "Use bash (for example sed or a short script) to replace this many."+ )return EditError(- f"Found {occurrences} occurrences of {which} in {path}, over the {MAX_REPLACEMENTS} replaceAll limit. "+ f"The edits make {replacements} replacements in {path}, over the {MAX_REPLACEMENTS} replacement limit. ""Use bash (for example sed or a short script) to replace this many.")+def _too_much_work(path: str, total: int, chars: int) -> EditError:+ return EditError(+ f"{total} edits over {path} ({chars} characters) are over the edit work limit. Split them into several "+ "edit calls, or use bash (for example sed or a short script)."+ )++def _empty(path: str, index: int, total: int) -> EditError:if total == 1:return EditError(f"oldText must not be empty in {path}.")@@ -228,15 +287,24 @@ class _Lines:return self.breaks[bisect.bisect_right(self.starts, lo) - 1] or self.first_breakdef with_breaks(self, lf_text: str, lo: int, own: Optional[list[str]] = None) -> str:- """``lf_text`` with each ``\\n`` turned into ``own``'s next break, then the break of the span at ``lo``."""+ """``lf_text`` with each ``\\n`` turned into ``own``'s next break, then the break of the span at ``lo``.++ Only the ``\\n`` that ``own`` covers (lines of the file) are visited one by one; the rest, which+ model text can make many of, are replaced in one pass without per-line objects.+ """if "\n" not in lf_text:return lf_text- pieces = lf_text.split("\n")fallback = self.break_for(lo)- out = [pieces[0]]- for index, piece in enumerate(pieces[1:]):- brk = own[index] if own is not None and index < len(own) and own[index] else fallback- out.append(brk + piece)+ out: list[str] = []+ pos = 0+ for brk in own or ():+ newline = lf_text.find("\n", pos)+ if newline == -1:+ break+ out += (lf_text[pos:newline], brk or fallback)+ pos = newline + 1+ rest = lf_text[pos:]+ out.append(rest if fallback == "\n" else rest.replace("\n", fallback))return "".join(out)@@ -301,25 +369,30 @@ def apply_edits(if not edit.old_text:raise _empty(path, index, total)+ if total * len(text) > MAX_EDIT_SCAN_CHARS:+ raise _too_much_work(path, total, len(text))lines = _Lines(text)- cache: list[str] = []+ cache: list[Optional[str]] = []- def normalized() -> str:+ def normalized() -> Optional[str]:+ """The normalized view, or ``None`` when NFKC would grow it past its budget (no loose matching)."""if not cache:- cache.append(normalize_for_fuzzy_match(lines.view))+ cache.append(_bounded_fuzzy_view(lines.view))return cache[0]exact: list[_Replacement] = []fuzzy: list[_Replacement] = []inserted = 0+ replacements_made = 0for index, edit in enumerate(edits):used_normalized, haystack, needle, count = _tier(lines.view, normalized, edit.old_text)if not count:- raise _not_found(path, index, total)+ raise _not_found(path, index, total, loose_unavailable=bool(cache) and cache[0] is None)if not edit.replace_all and count > 1:raise _duplicate(path, index, total, count)- if count > MAX_REPLACEMENTS:- raise _too_many(path, index, total, count)+ replacements_made += count+ if replacements_made > MAX_REPLACEMENTS:+ raise _too_many(path, total, replacements_made)# The result holds at least every inserted text, so this bound never refuses a result that fits.inserted += count * len(edit.new_text)if max_result_chars is not None and inserted > max_result_chars:@@ -329,7 +402,7 @@ def apply_edits()_check_disjoint(path, fuzzy) # in normalized coordinates, before they are grouped into lines- replacements = _exact_replacements(lines, exact) + (_line_groups(lines, normalized(), fuzzy) if fuzzy else [])+ replacements = _exact_replacements(lines, exact) + (_line_groups(lines, normalized() or "", fuzzy) if fuzzy else [])_check_disjoint(path, replacements)new_text = _apply(text, replacements)if new_text == text:@@ -344,13 +417,15 @@ def lf_view(text: str) -> str:#: The display diff runs difflib only on the changed middle, and skips it when either side is longer.DIFF_MAX_LINES = 1000+#: Lines either side may have before the display diff splits it into per-line strings at all.+DIFF_MAX_SPLIT_LINES = 100_000class _FixedOpcodes(difflib.SequenceMatcher):"""difflib's hunk grouping over opcodes computed elsewhere."""- def __init__(self, a: list[str], b: list[str], opcodes: list[tuple[str, int, int, int, int]]) -> None:- super().__init__(None, a, b)+ def __init__(self, opcodes: list[tuple[str, int, int, int, int]]) -> None:+ super().__init__(None, (), ()) # no sequences: difflib would index all of b, which grouping never usesself._fixed = opcodesdef get_opcodes(self) -> list[tuple[str, int, int, int, int]]: # type: ignore[override]@@ -364,6 +439,8 @@ def display_diff(path: str, old: str, new: str, context: int = 4) -> Optional[tuis quadratic, for example on repetitive lines) sees only the changed middle, at most``DIFF_MAX_LINES`` lines on each side."""+ if old.count("\n") > DIFF_MAX_SPLIT_LINES or new.count("\n") > DIFF_MAX_SPLIT_LINES:+ return Nonea, b = _lines_with_breaks(old), _lines_with_breaks(new)shortest = min(len(a), len(b))prefix = 0@@ -408,7 +485,7 @@ def _patch_line(sign: str, line: str) -> str:def _unified_patch(path: str, a: list[str], b: list[str], opcodes: list, context: int) -> str:"""Pi's ``generateUnifiedPatch`` (jsdiff ``createTwoFilesPatch`` with file headers only), from the opcodes."""- groups = list(_FixedOpcodes(a, b, opcodes).get_grouped_opcodes(context))+ groups = list(_FixedOpcodes(opcodes).get_grouped_opcodes(context))if not groups:return ""out = [f"--- {path}\n", f"+++ {path}\n"]diff --git a/core/agent_core/tools/job_wrapper.py b/core/agent_core/tools/job_wrapper.pyindex 83f1804eb..81ffee9f4 100644--- a/core/agent_core/tools/job_wrapper.py+++ b/core/agent_core/tools/job_wrapper.py@@ -85,7 +85,7 @@ def _wait_for_decision(job_dir: str, timeout_s: float) -> str:time.sleep(delay)delay = min(delay * 2, 0.05)with open(decision) as handle:- return handle.read().strip()+ return handle.read(64).strip()class _BoundedLog:diff --git a/core/agent_core/tools/jobs.py b/core/agent_core/tools/jobs.pyindex 9f874ec23..f1bd83ec0 100644--- a/core/agent_core/tools/jobs.py+++ b/core/agent_core/tools/jobs.py@@ -52,7 +52,7 @@ from typing import Any, Awaitable, Callable, Mapping, Optionalimport psutilfrom core.agent_core.tools.base import JobStatus-from core.agent_core.tools.paths import os_reason+from core.agent_core.tools.paths import os_reason, run_to_endfrom core.process_isolation import (DEFAULT_PROCESS_TERMINATE_TIMEOUT_SECONDS,KILL_SIGNAL,@@ -96,6 +96,10 @@ _JOB_ID = re.compile(r"^job_[a-z0-9]+$")_TAIL_HEADER = re.compile(rb"\[output from byte (\d+)\]\n")_GO = "go"_ABANDON = "abandon"+_STATE_FILE_CHARS = 4096+_SURROGATE = re.compile("[\ud800-\udfff]")+# The host's own meta.json holds the command, which a shell cannot even take past ARG_MAX (1 MiB here).+_META_CHARS = 4 * 1024 * 1024class JobStartError(RuntimeError):@@ -134,10 +138,26 @@ def _write_atomic(path: str, data: str) -> None:os.replace(tmp, path)+def _meta_text(meta: Mapping[str, Any]) -> str:+ """``meta.json`` as written: UTF-8 as it is (not escaped), and never past the bound ``meta`` reads in.++ A POSIX path can hold bytes that are not UTF-8, which Python keeps as surrogate escapes; UTF-8 cannot+ carry those, so such a record is written ASCII-escaped instead, which JSON reads back exactly.+ """+ text = json.dumps(meta, indent=2, ensure_ascii=False)+ if _SURROGATE.search(text):+ text = json.dumps(meta, indent=2)+ if len(text) > _META_CHARS:+ raise ValueError(f"the job record would be {len(text)} characters, over {_META_CHARS}")+ return text++def _read_text(path: str) -> Optional[str]:+ """A small state file (``pid``, ``decision``, ``exit``, ``stopped``), read in a bound: the job's+ directory is one its command can write to."""try:- with open(path, encoding="utf-8") as handle:- return handle.read().strip()+ with open(path, encoding="utf-8", errors="replace") as handle:+ return handle.read(_STATE_FILE_CHARS).strip()except FileNotFoundError:return None@@ -179,14 +199,19 @@ class LocalJobHost:return self._path(job_id, "output.log")def meta(self, job_id: str) -> dict[str, Any]:+ """The job's metadata. Read in a bound: the job's directory is one its command can write to, and a+ file over the bound is refused as corrupt (``ValueError``), as unreadable JSON is."""try:- with open(self._path(job_id, "meta.json"), encoding="utf-8") as handle:- return json.load(handle)+ with open(self._path(job_id, "meta.json"), encoding="utf-8", errors="replace") as handle:+ text = handle.read(_META_CHARS + 1)except FileNotFoundError:raise KeyError(job_id) from None+ if len(text) > _META_CHARS:+ raise ValueError(f"meta.json of job {job_id} is over {_META_CHARS} characters")+ return json.loads(text)def _write_meta(self, job_id: str, meta: Mapping[str, Any]) -> None:- _write_atomic(self._path(job_id, "meta.json"), json.dumps(meta, indent=2))+ _write_atomic(self._path(job_id, "meta.json"), _meta_text(meta))def find_job(self, session_id: str, tool_call_id: str) -> Optional[str]:"""The job started for a tool call, for settling it at resume."""@@ -263,10 +288,32 @@ class LocalJobHost:raise JobStartError("Command jobs are not supported on Windows yet.")if not sys.executable:raise JobStartError("No Python interpreter is available to run the command.")+ # The host's own steps are small and synchronous, so the only await is the wait for the pid: a cancel+ # can land nowhere else, and never between publishing ``go`` and returning the job id.+ job_id, proc, marker, meta = self._spawn(command, cwd, env, timeout_s, session_id, tool_call_id)+ try:+ identity = await self._await_pid(job_id, proc, marker)+ except asyncio.CancelledError:+ # Undecided: abandon the wrapper now rather than leave it waiting 30 s for a decision.+ self._create_decision(job_id, _ABANDON)+ raise+ decision = self._record_and_decide(job_id, meta, identity)+ if decision != _GO:+ raise JobStartError("The command did not start.")+ return job_id++ def _spawn(+ self,+ command: str,+ cwd: str,+ env: Mapping[str, str],+ timeout_s: Optional[float],+ session_id: str,+ tool_call_id: str,+ ) -> tuple[str, subprocess.Popen, str, dict[str, Any]]:+ """The job directory, ``meta.json``, and the wrapper, which waits for the decision (J1)."""job_id = f"job_{secrets.token_hex(8)}"job_dir = self.job_dir(job_id)- os.makedirs(self._jobs_dir, exist_ok=True)- os.mkdir(job_dir, 0o700)created = datetime.now(timezone.utc)deadline = _ceil_ms(created + timedelta(seconds=timeout_s)) if timeout_s is not None else Nonemeta: dict[str, Any] = {@@ -285,6 +332,15 @@ class LocalJobHost:"watch_id": None,"deadline_at": _iso(deadline) if deadline is not None else None,}+ try:+ # Checked before anything exists: a record the host could not read back would leave the job unmanaged.+ _meta_text(meta)+ except ValueError:+ raise JobStartError(+ f"Could not start the command: its job record would be over {_META_CHARS} characters."+ ) from None+ os.makedirs(self._jobs_dir, exist_ok=True)+ os.mkdir(job_dir, 0o700)self._write_meta(job_id, meta)marker = new_process_identity_marker()@@ -323,15 +379,16 @@ class LocalJobHost:os.close(diagnostics)self._children[job_id] = procthreading.Thread(target=self._reap, args=(job_id, proc), name=f"avibe-{job_id}-reaper", daemon=True).start()+ return job_id, proc, marker, meta- identity = await self._await_pid(job_id, proc, marker)+ def _record_and_decide(+ self, job_id: str, meta: dict[str, Any], identity: Optional[PersistedProcessIdentity]+ ) -> str:+ """Record the identity (J2), then decide: ``go`` only with an identity on record."""if identity is not None:meta["process"] = {**serialize_process_identity(identity), "pgid": identity.pid}self._write_meta(job_id, meta)- decision = self._create_decision(job_id, _GO if identity is not None else _ABANDON)- if decision != _GO:- raise JobStartError("The command did not start.")- return job_id+ return self._create_decision(job_id, _GO if identity is not None else _ABANDON)async def _await_pid(self, job_id: str, proc: subprocess.Popen, marker: str) -> Optional[PersistedProcessIdentity]:deadline = time.monotonic() + PID_TIMEOUT_S@@ -402,7 +459,7 @@ class LocalJobHost:return data, since + len(data)try:with open(self._path(job_id, "tail.log"), "rb") as handle:- header = _TAIL_HEADER.fullmatch(handle.readline())+ header = _TAIL_HEADER.fullmatch(handle.readline(64))if header is None:raise ValueError("tail.log header")tail_start = int(header.group(1))@@ -419,23 +476,35 @@ class LocalJobHost:``reason`` is recorded first (``stop_reason``), so whoever reports thejob later can say why it ended."""- if self._decision(job_id) != _GO:+ # The sequence runs to its end once begun: verification records ``stopped``, so stopping short of+ # the signals, or of the SIGKILL fallback, would leave a running group. A cancel is re-raised after.+ await run_to_end(self._kill(job_id, reason))++ async def _kill(self, job_id: str, reason: str) -> None:+ identity = self._verify_for_kill(job_id, reason)+ if identity is None:return+ if not await _terminate_group(identity.pid):+ logger.warning("Job %s process group %s survived termination", job_id, identity.pid)++ def _verify_for_kill(self, job_id: str, reason: str) -> Optional[PersistedProcessIdentity]:+ """The identity to signal, once the group is provably the job's, with ``reason`` recorded; else ``None``."""+ if self._decision(job_id) != _GO:+ return Noneidentity = self._identity(job_id)if identity is None:logger.warning("Job %s has no recorded process identity; not signaling", job_id)- return+ return Noneif not (self._alive(job_id) or self._group_carries_marker(identity)):if _group_exists(identity.pid):logger.warning("Job %s process group %s cannot be verified; not signaling", job_id, identity.pid)- return+ return Nonetry:self._create_once(job_id, "stopped", reason) # the first stopper's reason winsexcept OSError:# Recording is best effort at every stopper: the kill matters more than the record.logger.warning("Could not record why job %s was stopped", job_id, exc_info=True)- if not await asyncio.to_thread(_terminate_group, identity.pid):- logger.warning("Job %s process group %s survived termination", job_id, identity.pid)+ return identityasync 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).@@ -451,15 +520,18 @@ class LocalJobHost:# 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)+ self._record_watch(job_id, watch_id)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+ def _record_watch(self, job_id: str, watch_id: str) -> None:+ meta = self.meta(job_id)+ meta["watch_id"] = watch_id+ self._write_meta(job_id, meta)+# --- deadline (J3) and reporting --------------------------------------async def enforce_deadline(self, job_id: str) -> bool:@@ -469,14 +541,17 @@ class LocalJobHost:running job may already have ended on time: a live wrapper decides timeout versus exit. Thehost stops the job as the second owner once the wrapper has not, ``_WRAPPER_DECIDES_S`` late."""+ if not self._overdue(job_id):+ return False+ await self.kill(job_id, reason=STOP_TIMEOUT)+ return True++ def _overdue(self, job_id: str) -> bool:deadline_at = self.meta(job_id).get("deadline_at")if not deadline_at:return Falselate = (datetime.now(timezone.utc) - _parse_iso(deadline_at)).total_seconds()- if late < _WRAPPER_DECIDES_S or self.status(job_id).state != "running":- return False- await self.kill(job_id, reason=STOP_TIMEOUT)- return True+ return late >= _WRAPPER_DECIDES_S and self.status(job_id).state == "running"def stop_reason(self, job_id: str) -> Optional[str]:"""Why the host killed the job (``"timeout"``, or the caller's reason), if it did."""@@ -562,11 +637,13 @@ def _group_exists(pgid: int) -> bool:return True-def _terminate_group(pgid: int, timeout_s: float = DEFAULT_PROCESS_TERMINATE_TIMEOUT_SECONDS) -> bool:+async def _terminate_group(pgid: int, timeout_s: float = DEFAULT_PROCESS_TERMINATE_TIMEOUT_SECONDS) -> bool:"""SIGTERM the verified group, then SIGKILL what is left; ``True`` once the group is gone.The group was verified just before. A process group id is not reused whilethe group exists, so signaling it until it is gone reaches only its members.+ Signals go out from the event loop at once, never queued behind the shared+ worker threads; the waits between them are asynchronous."""if pgid <= 1 or pgid == os.getpgrp():logger.error("Refusing to signal process group %s", pgid)@@ -582,7 +659,7 @@ def _terminate_group(pgid: int, timeout_s: float = DEFAULT_PROCESS_TERMINATE_TIMwhile _group_exists(pgid):if time.monotonic() >= deadline:break- time.sleep(0.02)+ await asyncio.sleep(0.02)else:return Truereturn not _group_exists(pgid)diff --git a/core/agent_core/tools/normalize.py b/core/agent_core/tools/normalize.pyindex 0310595e3..d2d1c0847 100644--- a/core/agent_core/tools/normalize.py+++ b/core/agent_core/tools/normalize.py@@ -17,15 +17,23 @@ import refrom core.agent_core.tools.truncate import utf8_len+# No sequence spans a line break or a carriage return, so stripping a run of whole lines at once equals+# stripping each redraw segment of each line on its own._ANSI_RE = re.compile(- r"\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)" # OSC ... BEL or ST- r"|\x1b[P^_][^\x1b]*\x1b\\" # DCS, PM, APC ... ST+ r"\x1b\][^\x07\x1b\n\r]*(?:\x07|\x1b\\)" # OSC ... BEL or ST+ r"|\x1b[P^_][^\x1b\n\r]*\x1b\\" # DCS, PM, APC ... STr"|\x1b\[[0-?]*[ -/]*[@-~]" # CSIr"|\x1b[ -/]*[0-~]" # other escape sequencesr"|\x9b[0-?]*[ -/]*[@-~]" # 8-bit CSI)# An open line (no newline yet) keeps at most this many characters of its last segment._MAX_OPEN_CHARS = 64 * 1024+# A line longer than the bound, found from line starts only, so the search is linear.+_LONG_LINE = re.compile(r"^[^\n]{%d}" % (_MAX_OPEN_CHARS + 1), re.MULTILINE)+# A redrawn line keeps its last segment that shows something: drop the empty segments at its end,+# then everything up to its last carriage return.+_TRAILING_RETURNS = re.compile(r"\r+$", re.MULTILINE)+_BEFORE_LAST_RETURN = re.compile(r"^[^\n]*\r", re.MULTILINE)def _bounded(segment: str, dropped: int) -> tuple[str, int]:@@ -96,18 +104,37 @@ class OutputNormalizer:# Possibly the first half of "\r\n"; decide with the next chunk.self._carry_cr = Truetext = text[:-1]+ last = text.rfind("\n")+ if last == -1:+ self._open_segments(text)+ return ""+ first = text.find("\n")out: list[str] = []- lines = text.split("\n")- for line in lines[:-1]:- if line.endswith("\r"):- line = line[:-1]- self._open_segments(line)- text, self._finished_line_bytes = self._line_and_size()- out.append(text + "\n")- self._reset()- self._open_segments(lines[-1])+ self._finish_line(text[:first], out) # completes the open line+ if first != last:+ middle = text[first + 1 : last].replace("\r\n", "\n")+ if not _LONG_LINE.search(middle):+ # No line over the bound: each line becomes its last visible segment in a few passes over the+ # whole run, without a Python step per line, so a flood costs what it reads (redraws included).+ visible = strip_ansi(middle)+ if "\r" in visible:+ visible = _BEFORE_LAST_RETURN.sub("", _TRAILING_RETURNS.sub("", visible))+ out.append(visible + "\n")+ self._finished_line_bytes = utf8_len(visible[visible.rfind("\n") + 1 :])+ else:+ for line in middle.split("\n"):+ self._finish_line(line, out)+ self._open_segments(text[last + 1 :])return "".join(out)+ def _finish_line(self, line: str, out: list[str]) -> None:+ if line.endswith("\r"):+ line = line[:-1]+ self._open_segments(line)+ text, self._finished_line_bytes = self._line_and_size()+ out.append(text + "\n")+ self._reset()+def _open_segments(self, text: str) -> None:segments = (self._cur + text).split("\r")if len(segments) > 1:diff --git a/core/agent_core/tools/output.py b/core/agent_core/tools/output.pyindex 82c3b4e19..3b231809d 100644--- a/core/agent_core/tools/output.py+++ b/core/agent_core/tools/output.py@@ -9,15 +9,26 @@ output, so only the rolling tail and the counters remain.from __future__ import annotations+from concurrent.futures import ThreadPoolExecutorfrom dataclasses import replacefrom typing import Any, Optionalfrom core.agent_core.tools.base import MAX_BYTES, MAX_LINES, JobHostfrom core.agent_core.tools import jobs as job_hostfrom core.agent_core.tools.normalize import OutputNormalizer+from core.agent_core.tools.paths import run_joinedfrom core.agent_core.tools.truncate import TruncationResult, format_size, truncate_tail, utf8_len+#: Following output (normalizing, accumulating) has a pool of its own: asyncio's default executor is the+#: file tools', the host's pool is job control's, and none of them waits behind another.+_OUTPUT_IO = ThreadPoolExecutor(max_workers=8, thread_name_prefix="avibe-job-output")+#: Raw output one progress poll reads; the next poll continues where it stopped.+POLL_BYTES = 1024 * 1024+#: Raw output a final read takes: everything the log keeps on disk, and one more chunk.+FINISH_BYTES = job_host.OUTPUT_HEAD_BYTES + job_host.OUTPUT_TAIL_BYTES + job_host.OUTPUT_CHUNK_BYTES++class OutputAccumulator:"""Counts every line and byte of normalized output and keeps a tail of at most ``4 * max_bytes``."""@@ -98,10 +109,15 @@ class JobOutput:def path(self) -> str:return self._jobs.output_path(self._job_id)- def poll(self) -> bool:- """Read everything written so far; ``True`` if there was anything new."""+ def poll(self, max_bytes: int = POLL_BYTES) -> bool:+ """Read what was written since the last call, at most ``max_bytes`` of it; ``True`` if anything was new.++ The budget bounds one call however fast the job writes (a reader that falls behind the kept+ tail jumps forward, J4). Callers run it off the event loop, which every Session shares.+ """changed = False- while True:+ budget = max_bytes+ while budget > 0:data, offset = self._jobs.output(self._job_id, self._offset)skipped = offset - len(data) - self._offsetself._offset = offset@@ -112,13 +128,27 @@ class JobOutput:if not data:return changed or skipped > 0changed = True+ budget -= len(data)self._accumulator.append(self._normalizer.feed(data))+ return True++ async def poll_off_loop(self, max_bytes: int = POLL_BYTES) -> bool:+ """``poll`` on the output pool, off the event loop; a cancel waits for it."""+ return await run_joined(_OUTPUT_IO, self.poll, max_bytes)++ async def finish_off_loop(self) -> None:+ """``finish`` on the output pool, off the event loop; a cancel waits for it."""+ await run_joined(_OUTPUT_IO, self.finish)def finish(self) -> None:- """Read the rest and end the stream; call once the job is no longer running."""+ """Read the rest and end the stream; call once the job is no longer running.++ "The rest" is bounded by what the log keeps on disk: a background child still writing after the+ shell exited (ledger 11) cannot keep this reading forever.+ """if self._finished:return- self.poll()+ self.poll(FINISH_BYTES)self._accumulator.append(self._normalizer.flush())self._finished = Truediff --git a/core/agent_core/tools/paths.py b/core/agent_core/tools/paths.pyindex 2ab833502..db3d1fc0a 100644--- a/core/agent_core/tools/paths.py+++ b/core/agent_core/tools/paths.py@@ -10,13 +10,15 @@ from __future__ import annotationsimport asyncioimport contextlibimport errno+import functoolsimport ipaddressimport osimport reimport statimport unicodedatafrom contextlib import asynccontextmanager-from typing import Any, AsyncIterator, Callable, Literal, TypeVar+from concurrent.futures import Executor+from typing import Any, AsyncIterator, Awaitable, Callable, Literal, Optional, TypeVarfrom urllib.parse import unquotefrom core.agent_core.tools.args import ToolInputError@@ -297,7 +299,20 @@ async def to_thread_joined(func: Callable[..., T], /, *args: Any) -> T:the lock was released and overwrite the next writer's result. The cancel is re-raised once thethread is done, however often the caller is cancelled and even when the thread raised."""- worker = asyncio.ensure_future(asyncio.to_thread(func, *args))+ return await run_joined(None, func, *args)+++async def run_joined(executor: Optional[Executor], func: Callable[..., T], /, *args: Any) -> T:+ """``func(*args)`` on ``executor`` (``None``: asyncio's default); a cancel waits for it (see above)."""+ return await run_to_end(asyncio.get_running_loop().run_in_executor(executor, functools.partial(func, *args)))+++async def run_to_end(awaitable: Awaitable[T]) -> T:+ """Await ``awaitable`` to its end even if the caller is cancelled meanwhile, then re-raise the cancel.++ For state transitions that must not stop half-way once begun (a worker thread, a decision, a kill).+ """+ worker = asyncio.ensure_future(awaitable)try:return await asyncio.shield(worker)except asyncio.CancelledError:diff --git a/core/agent_core/tools/write.py b/core/agent_core/tools/write.pyindex d08255ef2..2f34c2118 100644--- a/core/agent_core/tools/write.py+++ b/core/agent_core/tools/write.py@@ -72,6 +72,8 @@ class WriteTool:return error_result(f"Cannot write {path}: its directory is not writable, so the file cannot be replaced safely.")+ except FileChanged:+ return error_result(f"Cannot write {path}: it changed while it was being written; write it again.")except NotRegularFile as exc:return error_result(f"Cannot write {path}: {KIND_REASON[exc.kind]}.")except OSError as exc:@@ -125,10 +127,12 @@ class FileIdentity:ino: intsize: intmtime_ns: int+ # Changes on every write and metadata change, and cannot be set back like mtime (rsync -t, tar).+ ctime_ns: int@classmethoddef of(cls, path: str, st: os.stat_result) -> "FileIdentity":- return cls(path, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns)+ return cls(path, st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, st.st_ctime_ns)def _require(expected: Optional[FileIdentity], path: str, st: os.stat_result) -> None:@@ -136,8 +140,12 @@ def _require(expected: Optional[FileIdentity], path: str, st: os.stat_result) ->raise FileChanged()-def _check_publishable(current: str, expected: Optional[FileIdentity]) -> None:- """Right before the rename: ``current`` is absent, or a writable regular file (the one that was read)."""+def _check_publishable(path: str, target: str, expected: Optional[FileIdentity]) -> None:+ """Right before the rename: ``path`` still names ``target``, which is absent or a writable regular file+ (for a read-modify-write, the one that was read)."""+ current = os.path.realpath(path)+ if current != target:+ raise FileChanged()try:st = os.stat(current)except FileNotFoundError:@@ -163,10 +171,11 @@ def write_bytes(path: str, data: bytes, expected: Optional[FileIdentity] = None)An existing file whose directory does not let a temp file be created is refused(``NotReplaceable``) rather than written in place, which a full disk could leave half-written.- With ``expected``, the file must still be the one that was read, checked once right before the- rename. Without it, the target is checked there again as ``write`` first classified it: absent, or a- writable regular file, so a file swapped for a FIFO, a directory, or a read-only file is never- replaced. POSIX has no compare-and-rename, so a change between that check and the rename is not seen.+ Right before the rename, the path must still name the same target, which must be absent or a+ writable regular file, as ``write`` first classified it; with ``expected``, it must also be the very+ file that was read. So a file swapped for a FIFO, a directory, or a read-only file, or a path+ retargeted to another file, is never written behind the result's back. POSIX has no+ compare-and-rename, so a change between that check and the rename is not seen."""target = os.path.realpath(path)try:@@ -188,7 +197,7 @@ def write_bytes(path: str, data: bytes, expected: Optional[FileIdentity] = None)os.fsync(handle.fileno())if mode is not None:os.fchmod(handle.fileno(), mode)- _check_publishable(os.path.realpath(path) if expected is not None else target, expected)+ _check_publishable(path, target, expected)os.replace(tmp, target)except BaseException:with contextlib.suppress(FileNotFoundError):diff --git a/docs/plans/agent-core-contracts/tools.md b/docs/plans/agent-core-contracts/tools.mdindex 7cb053ce5..a634a0a7d 100644--- a/docs/plans/agent-core-contracts/tools.md+++ b/docs/plans/agent-core-contracts/tools.md@@ -99,7 +99,10 @@ is true". Logic, from Pi's `edit-diff.ts`:1. Read the file inside the per-path mutation lock; strip the BOM; remember the line-ending style; normalize to LF.2. For each edit, find `oldText` exactly; if absent, find it after Pi's deterministic normalization (NFKC, trailingwhitespace per line, smart quotes to ASCII, Unicode dashes to `-`, special spaces to a space). No similarity- threshold.+ threshold. Avibe bound: NFKC can make one character eighteen, so normalization stops once it has grown the file by+ 1 Mi characters, or at a run of over 128 Ki characters with no ASCII character that is not already in NFKC (it is+ normalized in pieces split before ASCII characters); the not-found error then says that loose matching was not+ possible (below).3. Require exactly one occurrence unless `replaceAll`; reject empty `oldText`; reject overlapping spans, includingany occurrence matched by a `replaceAll` item.4. Apply all edits against the original, restore line endings and BOM, write once. Nothing is written if any edit@@ -107,7 +110,9 @@ is true". Logic, from Pi's `edit-diff.ts`:Result: `Successfully replaced {n} block(s) in {path}.`; the diff goes to `details` for display only. Errors:-- `Could not find edits[{i}] in {path}. The oldText must match exactly including all whitespace and newlines.`+- `Could not find edits[{i}] in {path}. The oldText must match exactly including all whitespace and newlines.`; when+ the normalization budget was reached, Avibe ends it with `: the file is too large to match it loosely after Unicode+ normalization.` instead of the period- `Found {count} occurrences of edits[{i}] in {path}. Each oldText must be unique. Please provide more context to make it unique.`- `edits[{a}] and edits[{b}] overlap in {path}. Merge them into one edit or target disjoint regions.`- `edits[{i}].oldText must not be empty in {path}.`diff --git a/tests/agent_core/tools/test_bash.py b/tests/agent_core/tools/test_bash.pyindex 1ed7b3346..db9a0f6d9 100644--- a/tests/agent_core/tools/test_bash.py+++ b/tests/agent_core/tools/test_bash.py@@ -8,12 +8,14 @@ import functoolsimport osimport signalimport time+from datetime import datetime, timezoneimport psutilimport pytestfrom core.agent_core.agent.jobs import TrackingJobHostfrom core.agent_core.cancel import CancelToken+import core.agent_core.tools.bash as bash_modulefrom core.agent_core.tools.bash import BashTool, settle_bash_callimport core.agent_core.tools.jobs as jobs_modulefrom core.agent_core.tools.jobs import LocalJobHost@@ -253,9 +255,8 @@ async def test_only_a_live_wrapper_decides_at_the_deadline(tmp_path, make_ctx, mmonkeypatch.setattr(jobs_module, "_terminate_group", quick_kill)host = _host(tmp_path)command = f"echo $$ > {tmp_path / 'sh.pid'}; sleep 30"- started = time.monotonic()if caller == "bash":- running = asyncio.ensure_future(BashTool(host).execute({"command": command, "timeout": 1.0}, make_ctx()))+ running = asyncio.ensure_future(BashTool(host).execute({"command": command, "timeout": 60}, make_ctx()))job_id = Nonewhile job_id is None:await asyncio.sleep(0.01)@@ -265,30 +266,112 @@ async def test_only_a_live_wrapper_decides_at_the_deadline(tmp_path, make_ctx, mcommand,cwd=str(tmp_path),env={"PATH": os.environ["PATH"]},- timeout_s=1.0,+ timeout_s=60,session_id="ses_test",tool_call_id="toolu_1",)running = asyncio.ensure_future(host.wait(job_id, deadline_s=None))wrapper_pid = os.path.join(host.job_dir(job_id), "pid")- while not (os.path.exists(wrapper_pid) and (tmp_path / "sh.pid").exists()):+ # Written, not only created: ``echo $$ >`` creates the file before it writes the pid.+ while not (os.path.exists(wrapper_pid) and (tmp_path / "sh.pid").exists() and (tmp_path / "sh.pid").read_text()):await asyncio.sleep(0.01)os.kill(int(open(wrapper_pid).read()), signal.SIGSTOP) # the wrapper can no longer decideshell_pid = int((tmp_path / "sh.pid").read_text())+ # The deadline passes now, whatever the launch took; the host reads it from meta.json.+ meta = host.meta(job_id)+ meta["deadline_at"] = jobs_module._iso(datetime.now(timezone.utc))+ host._write_meta(job_id, meta)- await asyncio.sleep(max(0.0, started + 1.3 - time.monotonic())) # past the deadline, inside the grace+ await asyncio.sleep(0.3) # past the deadline, inside the gracealive_in_grace = psutil.pid_exists(shell_pid) and psutil.Process(shell_pid).status() != psutil.STATUS_ZOMBIEoutcome = await asyncio.wait_for(running, timeout=10)assert alive_in_graceassert host.stop_reason(job_id) == "timeout"if caller == "bash":- assert result_text(outcome).endswith("Command timed out after 1 seconds")+ assert result_text(outcome).endswith("Command timed out after 60 seconds")else:assert outcome.state == "gone"assert _process_gone(shell_pid)[email protected]("command", ["yes", "yes $'a\\rb'"], ids=["lines", "redraws"])+async def test_a_flood_of_output_never_stalls_the_event_loop(tmp_path, make_ctx, command):+ """Every Session shares the event loop: following a job's output may not hold it, so an abort still lands."""+ cancel = CancelToken()+ lags: list[float] = []++ async def ticker():+ while True:+ before = time.monotonic()+ await asyncio.sleep(0.01)+ lags.append(time.monotonic() - before)++ ticking = asyncio.ensure_future(ticker())+ asyncio.get_running_loop().call_later(1.0, cancel.cancel)+ started = time.monotonic()+ try:+ result = await BashTool(_host(tmp_path)).execute(+ {"command": command}, make_ctx(cancel=cancel, on_progress=lambda tail: None)+ )+ finally:+ ticking.cancel()++ assert result_text(result).endswith("Command aborted")+ assert time.monotonic() - started < 6.0+ # Following the output costs the loop about 0.1 s at worst (it took 3 to 5 s on the loop before);+ # the host's own small reads may add a slow disk's latency on top (ledger A56).+ assert max(lags) < 1.5, max(lags)+++async def test_a_result_never_waits_behind_busy_file_tools(tmp_path, make_ctx):+ """asyncio's default executor is the file tools'; following a job's output has a pool of its own."""+ import threading++ release = threading.Event()+ busy = [asyncio.ensure_future(asyncio.to_thread(release.wait, 10)) for _ in range(64)]+ started = time.monotonic()+ try:+ result = await BashTool(_host(tmp_path)).execute({"command": "echo hi"}, make_ctx())+ took = time.monotonic() - started+ finally:+ release.set()+ await asyncio.gather(*busy)++ assert result_text(result) == "hi\n"+ assert took < 3.0+++async def test_a_handover_shows_the_latest_output_however_much_came_before(tmp_path):+ """A one-shot render reads the whole retained log: head, the omitted middle if any, and the tail.++ A fresh ``JobOutput`` (settlement, a recovery handover) renders a job whose end is already on disk.+ """+ host = _host(tmp_path, Watches())+ job_id = await host.start(+ "seq 1 300000; sleep 30",+ cwd=str(tmp_path),+ env={"PATH": os.environ["PATH"]},+ timeout_s=None,+ session_id="ses_test",+ tool_call_id="toolu_1",+ )+ tail = os.path.join(host.job_dir(job_id), "tail.log")+ try:+ for _ in range(500): # the wrapper snapshots tail.log four times a second+ if os.path.exists(tail) and open(tail, "rb").read().endswith(b"\n300000\n"):+ break+ await asyncio.sleep(0.02)++ result = await settle_bash_call(host, "ses_test", "toolu_1")++ text = result_text(result)+ assert text.startswith("Command is still running and is now Watch wch_1.")+ assert "\n300000\n" in text+ finally:+ await host.kill(job_id)++async def test_the_foreground_window_hands_over_and_the_command_runs_once(tmp_path, make_ctx):watches = Watches()host = _host(tmp_path, watches)@@ -313,19 +396,30 @@ async def test_the_foreground_window_hands_over_and_the_command_runs_once(tmp_paassert watches.by_job == {job_id: "wch_1"}-async def test_watch_true_returns_at_once(tmp_path, make_ctx):+async def test_watch_true_returns_at_once(tmp_path, make_ctx, monkeypatch):+ """At once after the command may run (the 0.5 s grace), however long the launch itself took."""host = _host(tmp_path, Watches())- started = time.monotonic()+ may_run_at = []+ real_start = host.start++ async def start_and_note(*args, **kwargs):+ job_id = await real_start(*args, **kwargs)+ may_run_at.append(time.monotonic())+ return job_id++ monkeypatch.setattr(host, "start", start_and_note)result = await BashTool(host).execute({"command": "sleep 2; echo late", "watch": True}, make_ctx())- assert time.monotonic() - started < 1.5+ assert time.monotonic() - may_run_at[0] < 1.5assert result_text(result).startswith("Command is still running and is now Watch wch_1.")assert host.status(result.details["job_id"]).state == "running"await host.kill(result.details["job_id"])-async def test_watch_true_keeps_the_result_of_a_command_that_already_ended(tmp_path, make_ctx):+async def test_watch_true_keeps_the_result_of_a_command_that_already_ended(tmp_path, make_ctx, monkeypatch):+ """A handover is decided only after a fresh status, so a command that has ended keeps its own result."""+ monkeypatch.setattr(bash_module, "_WATCH_GRACE_S", 30.0) # the command surely ends within itwatches = Watches()result = await BashTool(_host(tmp_path, watches)).execute({"command": "exit 3", "watch": True}, make_ctx())@@ -384,9 +478,12 @@ async def test_without_a_watch_the_command_stays_in_the_foreground(tmp_path, makasync def test_settlement_reports_each_job_state(tmp_path, make_ctx):watches = Watches()host = _host(tmp_path, watches)- tool = BashTool(host, foreground_window_s=0.2)- foreground = await tool.execute({"command": "echo out; exit 4"}, make_ctx(tool_call_id="exited"))- await tool.execute({"command": "echo still; sleep 30"}, make_ctx(tool_call_id="running"))+ # Each call's outcome is fixed by construction, not by timing: the exited one runs with the default+ # window (it cannot be handed over before it ends), the running one never ends.+ foreground = await BashTool(host).execute({"command": "echo out; exit 4"}, make_ctx(tool_call_id="exited"))+ await BashTool(host, foreground_window_s=0.2).execute(+ {"command": "echo still; sleep 30"}, make_ctx(tool_call_id="running")+ )recovery = LocalJobHost(str(tmp_path / "jobs"), on_hand_over=watches)@@ -394,7 +491,10 @@ async def test_settlement_reports_each_job_state(tmp_path, make_ctx):assert (result_text(exited), exited.is_error) == (result_text(foreground), True)running = await settle_bash_call(recovery, "ses_test", "running")assert result_text(running).startswith("Command is still running and is now Watch wch_1.")- assert result_text(await settle_bash_call(recovery, "ses_test", "running")) == result_text(running)+ # Settling again adopts the same Watch. (The output may still be arriving, so only the Watch is compared.)+ again = await settle_bash_call(recovery, "ses_test", "running")+ assert result_text(again).startswith("Command is still running and is now Watch wch_1.")+ assert again.details["watch_id"] == running.details["watch_id"] == "wch_1"assert await settle_bash_call(recovery, "ses_test", "unknown") is Noneawait recovery.kill(running.details["job_id"])assert await settle_bash_call(recovery, "ses_test", "running") is None@@ -403,16 +503,25 @@ async def test_settlement_reports_each_job_state(tmp_path, make_ctx):async def test_a_timeout_holds_across_handover_and_a_restart(tmp_path, make_ctx):"""J3: the deadline is the job's, so the next owner enforces it."""result = await BashTool(_host(tmp_path, Watches())).execute(- {"command": "echo begun; sleep 30", "timeout": 1.5, "watch": True}, make_ctx()+ {"command": "echo begun; sleep 30", "timeout": 60, "watch": True}, make_ctx())job_id = result.details["job_id"]assert result_text(result).startswith("Command is still running")-restarted = LocalJobHost(str(tmp_path / "jobs"))- status = await asyncio.wait_for(restarted.wait(job_id, deadline_s=None), timeout=5)+ output = restarted.output_path(job_id)+ for _ in range(500):+ if open(output).read() == "begun\n":+ break+ await asyncio.sleep(0.02)+ # The deadline passes now, whatever the launch took; only the restarted host is left to enforce it.+ meta = restarted.meta(job_id)+ meta["deadline_at"] = jobs_module._iso(datetime.now(timezone.utc))+ restarted._write_meta(job_id, meta)++ status = await asyncio.wait_for(restarted.wait(job_id, deadline_s=None), timeout=10)assert status.state == "gone"assert restarted.stop_reason(job_id) == "timeout"settled = await settle_bash_call(restarted, "ses_test", "toolu_1")- assert result_text(settled) == "begun\n\n\nCommand timed out after 1.5 seconds"+ assert result_text(settled) == "begun\n\n\nCommand timed out after 60 seconds"assert not os.path.exists(os.path.join(restarted.job_dir(job_id), "exit"))diff --git a/tests/agent_core/tools/test_jobs.py b/tests/agent_core/tools/test_jobs.pyindex 4bbe9e615..8bca4f39a 100644--- a/tests/agent_core/tools/test_jobs.py+++ b/tests/agent_core/tools/test_jobs.py@@ -14,6 +14,7 @@ import osimport signalimport subprocessimport time+from datetime import datetime, timezoneimport psutilimport pytest@@ -57,6 +58,12 @@ def _wrapper_gone(job_dir):return True+def _pid_written(path, timeout_s=10.0):+ """The pid a command wrote to ``path``: waits until it is written, not only created (``echo $$ >``)."""+ assert _wait_until(lambda: path.exists() and path.read_text().strip(), timeout_s=timeout_s)+ return int(path.read_text())++def _gone(pid):try:return psutil.Process(pid).status() == psutil.STATUS_ZOMBIE@@ -215,16 +222,17 @@ async def test_the_wrapper_enforces_the_deadline_while_no_host_runs(tmp_path):"""J3: the host crashed right after the start; the deadline still ends the tree and is recorded."""host = LocalJobHost(str(tmp_path / "jobs"))job_id = await _start(host, tmp_path, f"echo $$ > {tmp_path / 'sh.pid'}; sleep 30 & sleep 30", timeout_s=0.5)- shell_pid = None- assert _wait_until(lambda: (tmp_path / "sh.pid").exists())- shell_pid = int((tmp_path / "sh.pid").read_text())del host # nobody waits on the job from here on- assert _wait_until(lambda: _wrapper_gone(str(tmp_path / "jobs" / job_id)), timeout_s=6)+ # Either the shell ran and the deadline stopped its tree, or (on a slow launch) the deadline passed+ # first and the command never started: the wrapper owns both, and records the timeout either way.+ assert _wait_until(lambda: _wrapper_gone(str(tmp_path / "jobs" / job_id)), timeout_s=10)restarted = LocalJobHost(str(tmp_path / "jobs"))assert restarted.status(job_id).state == "gone"assert restarted.stop_reason(job_id) == "timeout"- assert not psutil.pid_exists(shell_pid) or psutil.Process(shell_pid).status() == psutil.STATUS_ZOMBIE+ if (tmp_path / "sh.pid").exists():+ shell_pid = _pid_written(tmp_path / "sh.pid")+ assert _wait_until(lambda: _gone(shell_pid))async def test_a_wrapper_that_cannot_keep_the_log_stops_its_command(tmp_path, monkeypatch):@@ -307,8 +315,7 @@ async def test_a_kill_whose_reason_cannot_be_recorded_still_stops_the_command(tm"""Recording why is best effort at every stopper: a full disk must not leave the command running."""host = LocalJobHost(str(tmp_path / "jobs"))job_id = await _start(host, tmp_path, f"echo $$ > {tmp_path / 'sh.pid'}; sleep 30")- assert _wait_until(lambda: (tmp_path / "sh.pid").exists())- shell_pid = int((tmp_path / "sh.pid").read_text())+ shell_pid = _pid_written(tmp_path / "sh.pid")real_create = host._create_oncedef full_disk(job_id, name, value):@@ -354,8 +361,7 @@ async def test_a_signal_to_the_wrapper_alone_stops_its_command(tmp_path, sig):"""``pkill -f python`` reaches the wrapper but not its shell: the wrapper must take the group with it."""host = LocalJobHost(str(tmp_path / "jobs"))job_id = await _start(host, tmp_path, f"echo $$ > {tmp_path / 'sh.pid'}; sleep 30", timeout_s=60)- assert _wait_until(lambda: (tmp_path / "sh.pid").exists())- shell_pid = int((tmp_path / "sh.pid").read_text())+ shell_pid = _pid_written(tmp_path / "sh.pid")os.kill(int(open(os.path.join(host.job_dir(job_id), "pid")).read()), sig)@@ -374,11 +380,14 @@ async def test_a_command_outliving_a_killed_wrapper_is_still_running_and_held_to# Python as the shell: its environment, and so the marker, is readable on macOS too.host = LocalJobHost(str(tmp_path / "jobs"), shell=sys.executable)script = f"import os, time; open({str(tmp_path / 'sh.pid')!r}, 'w').write(str(os.getpid())); time.sleep(30)"- job_id = await _start(host, tmp_path, script, timeout_s=1.0)- assert _wait_until(lambda: (tmp_path / "sh.pid").exists() and (tmp_path / "sh.pid").read_text())- shell_pid = int((tmp_path / "sh.pid").read_text())+ job_id = await _start(host, tmp_path, script, timeout_s=60)+ shell_pid = _pid_written(tmp_path / "sh.pid")os.kill(int(open(os.path.join(host.job_dir(job_id), "pid")).read()), signal.SIGKILL)assert _wait_until(lambda: _wrapper_gone(host.job_dir(job_id)))+ # Only the host is left to enforce the deadline, from meta.json: bring it to now, whatever the launch took.+ meta = host.meta(job_id)+ meta["deadline_at"] = jobs_module._iso(datetime.now(timezone.utc))+ host._write_meta(job_id, meta)try:assert host.status(job_id).state == "running"@@ -432,6 +441,156 @@ async def test_hosts_over_different_spellings_of_the_jobs_directory_see_the_sameawait host.kill(job_id)+async def test_readers_of_job_files_bound_what_they_read(tmp_path):+ """Job files sit where the command can write: a state file or tail.log header of any size is read in a bound."""+ import tracemalloc++ host = LocalJobHost(str(tmp_path / "jobs"))+ job_id = await _start(host, tmp_path, "true")+ await host.wait(job_id, deadline_s=5)+ job_dir = host.job_dir(job_id)+ with open(os.path.join(job_dir, "stopped"), "w") as handle:+ handle.write("x" * 20_000_000)+ with open(os.path.join(job_dir, "tail.log"), "w") as handle:+ handle.write("[output from byte 0" + "0" * 20_000_000)+ tracemalloc.start()+ try:+ reason = host.stop_reason(job_id)+ data, offset = host.output(job_id, since=os.path.getsize(host.output_path(job_id)))+ peak = tracemalloc.get_traced_memory()[1]+ finally:+ tracemalloc.stop()++ assert peak < 1024 * 1024+ assert len(reason) <= 4096+ assert (data, offset) == (b"", os.path.getsize(host.output_path(job_id)))+++async def test_a_kill_signals_at_once_even_when_worker_threads_are_busy(tmp_path):+ """The default executor is shared with every file tool: a kill must not queue behind it."""+ import threading++ host = LocalJobHost(str(tmp_path / "jobs"))+ job_id = await _start(host, tmp_path, f"echo $$ > {tmp_path / 'sh.pid'}; sleep 30")+ shell_pid = _pid_written(tmp_path / "sh.pid")+ release = threading.Event()+ busy = [asyncio.ensure_future(asyncio.to_thread(release.wait, 10)) for _ in range(64)]+ try:+ killing = asyncio.ensure_future(host.kill(job_id, reason="aborted"))+ deadline = time.monotonic() + 1.0+ while time.monotonic() < deadline and not _gone(shell_pid):+ await asyncio.sleep(0.02)+ gone_in_time = _gone(shell_pid)+ finally:+ release.set()+ await asyncio.gather(*busy)+ await killing++ assert gone_in_time+++async def test_a_cancelled_kill_still_finishes_the_group(tmp_path):+ """Once the first signal is sent, the SIGKILL fallback runs even if the caller is cancelled meanwhile."""+ import sys++ host = LocalJobHost(str(tmp_path / "jobs"), shell=sys.executable)+ script = (+ "import os, signal, time; signal.signal(signal.SIGTERM, signal.SIG_IGN); "+ f"open({str(tmp_path / 'sh.pid')!r}, 'w').write(str(os.getpid())); time.sleep(60)"+ )+ job_id = await _start(host, tmp_path, script)+ shell_pid = _pid_written(tmp_path / "sh.pid")+ os.kill(int(open(os.path.join(host.job_dir(job_id), "pid")).read()), signal.SIGKILL) # only the host is left+ assert _wait_until(lambda: _wrapper_gone(host.job_dir(job_id)))++ killing = asyncio.ensure_future(host.kill(job_id, reason="aborted"))+ await asyncio.sleep(0.5) # SIGTERM is ignored; the host is waiting before SIGKILL+ killing.cancel()+ with pytest.raises(asyncio.CancelledError):+ await killing++ assert _wait_until(lambda: _gone(shell_pid), timeout_s=8)+++ ("command", "starts"),+ [+ ("# " + "\x01" * 1000 + "\ntrue", False), # JSON escapes each control character to six characters+ ("# " + "\u4e2d" * 1000 + "\ntrue", True), # written as it is, not escaped+ ],+ ids=["escapes-past-the-bound", "non-ascii"],+)+async def test_a_command_whose_record_would_be_unreadable_never_starts(tmp_path, monkeypatch, command, starts):+ """The host reads meta.json in a bound, so it never writes one past it: such a command is refused unstarted."""+ monkeypatch.setattr(jobs_module, "_META_CHARS", 2000)+ host = LocalJobHost(str(tmp_path / "jobs"))++ if not starts:+ with pytest.raises(JobStartError):+ await _start(host, tmp_path, command)+ assert host.find_job("ses_test", "toolu_1") is None+ return+ job_id = await _start(host, tmp_path, command)+ assert (await host.wait(job_id, deadline_s=5)).state == "exited"+ assert host.meta(job_id)["command"] == command+++async def test_a_start_cancelled_before_its_decision_abandons_the_wrapper_at_once(tmp_path, monkeypatch):+ """The only await in ``start`` is the wait for the pid: a cancel there abandons the wrapper, so it+ exits now instead of waiting 30 s for a decision, and the command never runs."""+ host = LocalJobHost(str(tmp_path / "jobs"))+ entered = asyncio.Event()+ real_await_pid = LocalJobHost._await_pid++ async def slow_await_pid(self, *args):+ entered.set()+ await asyncio.sleep(1.0)+ return await real_await_pid(self, *args)++ monkeypatch.setattr(LocalJobHost, "_await_pid", slow_await_pid)+ task = asyncio.ensure_future(_start(host, tmp_path, f"echo ran > {tmp_path / 'ran'}"))+ await entered.wait()+ task.cancel()+ with pytest.raises(asyncio.CancelledError):+ await task++ job_id = host.find_job("ses_test", "toolu_1")+ assert host.started(job_id) is False+ assert _wait_until(lambda: _wrapper_gone(host.job_dir(job_id)), timeout_s=5)+ assert not (tmp_path / "ran").exists()+++async def test_a_cwd_with_bytes_that_are_not_utf8_is_recorded_and_read_back(tmp_path):+ """POSIX paths are bytes: Python keeps a non-UTF-8 byte as a surrogate escape, which meta.json must keep too."""+ cwd = str(tmp_path / "d\udcff") # what os.fsdecode gives for b"d\xff" (not created: APFS refuses the name)+ host = LocalJobHost(str(tmp_path / "jobs"))++ with pytest.raises(JobStartError): # the directory does not exist; the record must not be what fails+ await host.start("true", cwd=cwd, env=_env(), timeout_s=None, session_id="ses_test", tool_call_id="toolu_1")++ assert host.meta(host.find_job("ses_test", "toolu_1"))["cwd"] == cwd+++async def test_meta_json_is_read_in_a_bound(tmp_path):+ """The job directory is the command's to write: a huge meta.json is refused as corrupt, not loaded."""+ import tracemalloc++ host = LocalJobHost(str(tmp_path / "jobs"))+ job_id = await _start(host, tmp_path, "true")+ await host.wait(job_id, deadline_s=5)+ with open(os.path.join(host.job_dir(job_id), "meta.json"), "w") as handle:+ handle.write('{"pad": "' + "x" * 20_000_000 + '"}')+ tracemalloc.start()+ try:+ with pytest.raises(ValueError):+ host.meta(job_id)+ peak = tracemalloc.get_traced_memory()[1]+ finally:+ tracemalloc.stop()++ assert peak < 16 * 1024 * 1024 # the 4 Mi-character bound, read and decoded; the file is 20 MB++def test_the_wrapper_stops_its_group_even_when_its_diagnostics_fail(tmp_path, monkeypatch):"""The wrapper's own failure handler must stop the command even if writing the traceback fails (ENOSPC)."""import core.agent_core.tools.job_wrapper as wrapper@@ -466,6 +625,8 @@ async def test_job_files_stay_until_the_call_is_settled(tmp_path):finished = await _start(host, tmp_path, "true", tool_call_id="finished")running = await _start(host, tmp_path, "sleep 30", tool_call_id="running")await host.wait(finished, deadline_s=5)+ # An exited job is kept while its wrapper still runs (it may drain children); this one has none.+ assert _wait_until(lambda: _wrapper_gone(host.job_dir(finished)))assert host.prune(lambda meta: False, older_than_s=0) == []assert host.prune(lambda meta: True, older_than_s=0) == [finished]diff --git a/tests/agent_core/tools/test_read.py b/tests/agent_core/tools/test_read.pyindex 511e1c301..cb966a9c2 100644--- a/tests/agent_core/tools/test_read.py+++ b/tests/agent_core/tools/test_read.py@@ -284,9 +284,9 @@ async def test_a_huge_png_shaped_file_is_classified_without_walking_it(tmp_path,with open(path, "wb") as handle:handle.write(_png()[:33]) # signature and IHDR, then nothing but zero byteshandle.truncate(64 * 1024 * 1024)- started = time.monotonic()+ started = time.process_time()result = await ReadTool().execute({"path": "huge.png"}, make_ctx())- assert time.monotonic() - started < 1.0+ assert time.process_time() - started < 1.0assert result_text(result).startswith("Read image file [image/png]\n[Image omitted: the file is 64.0MB")diff --git a/tests/agent_core/tools/test_write_edit.py b/tests/agent_core/tools/test_write_edit.pyindex 349ddedad..7866c56d6 100644--- a/tests/agent_core/tools/test_write_edit.py+++ b/tests/agent_core/tools/test_write_edit.py@@ -248,7 +248,7 @@ async def test_a_cancelled_write_lands_before_the_next_writer_takes_the_lock(assert (tmp_path / "f.txt").read_text() == "second\n"[email protected]("change", ["replaced", "retargeted", "rewritten"])[email protected]("change", ["replaced", "retargeted", "rewritten", "rewritten-same-size-same-mtime"])async def test_an_edit_publishes_only_over_the_file_it_read(tmp_path, make_ctx, monkeypatch, change):"""Another writer (bash, an editor) changed the file after edit read it: nothing is written over it."""(tmp_path / "d").mkdir()@@ -266,8 +266,13 @@ async def test_an_edit_publishes_only_over_the_file_it_read(tmp_path, make_ctx,elif change == "retargeted":link.unlink()link.symlink_to(other)- else:+ elif change == "rewritten":real.write_text("unrelated text\n")+ else:+ # Same size, in place, mtime put back (as rsync -t or tar do): only ctime still shows it.+ before = os.stat(real)+ real.write_text("ALPHA\n")+ os.utime(real, ns=(before.st_atime_ns, before.st_mtime_ns))return plannedmonkeypatch.setattr(edit_module, "_plan_edits", plan_then_change)@@ -277,7 +282,10 @@ async def test_an_edit_publishes_only_over_the_file_it_read(tmp_path, make_ctx,True,"Could not edit file: d/f.txt. It changed while the edit was being applied; read it again.",)- expected = {"retargeted": ("alpha\n", "other\n")}.get(change, ("unrelated text\n", "other\n"))+ expected = {+ "retargeted": ("alpha\n", "other\n"),+ "rewritten-same-size-same-mtime": ("ALPHA\n", "other\n"),+ }.get(change, ("unrelated text\n", "other\n"))assert (real.read_text(), other.read_text()) == expected@@ -328,6 +336,31 @@ async def test_a_name_at_the_length_limit_can_be_replaced(tmp_path, make_ctx, toassert (tmp_path / name).read_text() == "new\n"[email protected]("tool", ["write", "edit"])+async def test_a_path_retargeted_during_the_write_is_not_written(tmp_path, make_ctx, monkeypatch, tool):+ """The path names another file by the time of the rename: the old target is not written behind its back."""+ (tmp_path / "a.txt").write_text("alpha\n")+ (tmp_path / "b.txt").write_text("beta\n")+ (tmp_path / "f.txt").symlink_to(tmp_path / "a.txt")+ real_fsync = write_module.os.fsync++ def fsync_then_retarget(fd):+ real_fsync(fd)+ (tmp_path / "f.txt").unlink()+ (tmp_path / "f.txt").symlink_to(tmp_path / "b.txt")++ monkeypatch.setattr(write_module.os, "fsync", fsync_then_retarget)+ if tool == "write":+ result = await WriteTool().execute({"path": "f.txt", "content": "new\n"}, make_ctx())+ expected = "Cannot write f.txt: it changed while it was being written; write it again."+ else:+ result = await _edit(make_ctx, "f.txt", {"oldText": "alpha", "newText": "new"})+ expected = "Could not edit file: f.txt. It changed while the edit was being applied; read it again."++ assert (result.is_error, result_text(result)) == (True, expected)+ assert ((tmp_path / "a.txt").read_text(), (tmp_path / "b.txt").read_text()) == ("alpha\n", "beta\n")++async def test_new_files_keep_the_umask_default(tmp_path, make_ctx):umask = os.umask(0)os.umask(umask)@@ -349,11 +382,11 @@ async def test_write_and_edit_sanitize_model_text_the_same_way(tmp_path, make_ctasync def test_the_display_diff_is_bounded_by_lines_not_only_bytes(tmp_path, make_ctx):"""Repetitive lines made difflib quadratic (Codex measured over 22 s); the changed middle is all it sees."""(tmp_path / "rep.txt").write_text("x\n" * 5000 + "target\n" + "x\n" * 5000)- started = time.monotonic()+ started = time.process_time()result = await _edit(make_ctx, "rep.txt", {"oldText": "target", "newText": "changed"})- assert time.monotonic() - started < 2.0+ assert time.process_time() - started < 2.0assert not result.is_error, result_text(result)# Pi pads line numbers to the widest one (five digits here).assert "- 5001 target\n+ 5001 changed" in result.details["diff"]@@ -427,12 +460,13 @@ async def test_the_display_diff_and_patch_are_pis(tmp_path, make_ctx, original,ids=["whitespace-run", "replace-all"],)async def test_edit_work_stays_linear_on_adversarial_files(tmp_path, make_ctx, content, edit):+ """CPU time, which a quadratic match would take seconds of; disk waits do not count."""(tmp_path / "f.txt").write_text(content)- started = time.monotonic()+ started = time.process_time()await _edit(make_ctx, "f.txt", edit)- assert time.monotonic() - started < 1.0+ assert time.process_time() - started < 1.0@pytest.mark.parametrize(@@ -446,8 +480,10 @@ async def test_edit_work_stays_linear_on_adversarial_files(tmp_path, make_ctx, c("x" * 1_000_000, {"oldText": "x", "newText": "y", "replaceAll": True}, True),# A result of three million lines, within the byte limit: its view is built without per-line state.("t," * 10, {"oldText": "t", "newText": "\n" * 300_000, "replaceAll": True}, False),+ # oldText with more lines than the file cannot match, and is never normalized line by line.+ ("zz\n", {"oldText": "ab\n" * 1_000_000, "newText": "y"}, True),],- ids=["duplicate", "many-lines", "many-occurrences", "many-result-lines"],+ ids=["duplicate", "many-lines", "many-occurrences", "many-result-lines", "long-old-text"],)async def test_edit_memory_is_bounded_by_its_budgets_not_by_the_file(tmp_path, make_ctx, content, edit, is_error):"""Python objects per line or per match cost hundreds of bytes each, in the process every Session shares."""@@ -463,6 +499,54 @@ async def test_edit_memory_is_bounded_by_its_budgets_not_by_the_file(tmp_path, massert peak < 32 * 1024 * 1024+ "content",+ ["\ufdfa" * 1_100_000 + "\n", "".join("\ufdfa" * 1000 + "\n" for _ in range(1100))],+ ids=["one-long-line", "many-lines"],+)+async def test_nfkc_expansion_has_a_budget_and_says_so(tmp_path, make_ctx, content):+ """NFKC can make one character eighteen (U+FDFA): loose matching stops at a budget and says why."""+ (tmp_path / "f.txt").write_text(content + "\u2019marker\u2019\n")+ tracemalloc.start()+ started = time.process_time()+ try:+ result = await _edit(make_ctx, "f.txt", {"oldText": "'marker'", "newText": "x"})+ peak = tracemalloc.get_traced_memory()[1]+ finally:+ tracemalloc.stop()++ assert (result.is_error, result_text(result)) == (+ True,+ "Could not find the exact text in f.txt. The old text must match exactly including all whitespace and "+ "newlines: the file is too large to match it loosely after Unicode normalization.",+ )+ assert peak < 48 * 1024 * 1024+ assert time.process_time() - started < 3.0+++async def test_a_large_file_with_a_few_compatibility_characters_still_matches_loosely(tmp_path, make_ctx):+ """An ellipsis (NFKC "...") in a 2 MiB file costs a few characters of growth, not the fuzzy tier."""+ body = "".join(f"line {i} \u2026 text\n" for i in range(110_000)) # about 2 MiB, not in NFKC+ (tmp_path / "f.txt").write_text(body + "say \u2018marker\u2019\n")++ result = await _edit(make_ctx, "f.txt", {"oldText": "say 'marker'", "newText": "said"})++ assert not result.is_error, result_text(result)+ assert (tmp_path / "f.txt").read_text().endswith("\u2026 text\nsaid\n")+++async def test_a_long_minified_line_still_matches_loosely(tmp_path, make_ctx):+ """A 200 KiB line is normalized in pieces split before ASCII characters, which NFKC never composes with."""+ line = "x=1;" * 50_000 + "\u2026 say \u2018marker\u2019"+ (tmp_path / "f.min.js").write_text(line + "\n")++ result = await _edit(make_ctx, "f.min.js", {"oldText": "say 'marker'", "newText": "said"})++ assert not result.is_error, result_text(result)+ # A normalized match rewrites its whole line from the normalized view (ledger 13), so "…" became "...".+ assert (tmp_path / "f.min.js").read_text() == "x=1;" * 50_000 + "... said\n"++async def test_edit_refuses_files_with_more_lines_than_it_plans(tmp_path, make_ctx, monkeypatch):monkeypatch.setattr(edit_module, "MAX_EDIT_LINES", 100)(tmp_path / "ok.txt").write_text("x\n" * 99 + "t")@@ -488,8 +572,12 @@ async def test_edit_refuses_files_with_more_lines_than_it_plans(tmp_path, make_c"Use bash (for example sed or a short script) to replace this many.",),(- [{"oldText": "zz", "newText": "q"}, {"oldText": "a", "newText": "b", "replaceAll": True}],- "Found 11 occurrences of edits[1] in f.txt, over the 10 replaceAll limit. "+ # The budget is for the whole call: 11 + 10 replacements by two edits, each under it alone.+ [+ {"oldText": "a", "newText": "b", "replaceAll": True},+ {"oldText": "c", "newText": "d", "replaceAll": True},+ ],+ "The edits make 21 replacements in f.txt, over the 19 replacement limit. ""Use bash (for example sed or a short script) to replace this many.",),(@@ -499,11 +587,11 @@ async def test_edit_refuses_files_with_more_lines_than_it_plans(tmp_path, make_c"Use bash (for example sed or a short script) to change files this large.",),],- ids=["single", "batch", "inserted-text"],+ ids=["single", "batch-total", "inserted-text"],)async def test_replace_all_has_a_budget(tmp_path, make_ctx, monkeypatch, edits, message):monkeypatch.setattr(edit_module, "MAX_EDIT_BYTES", 1000)- monkeypatch.setattr(edit_diff_module, "MAX_REPLACEMENTS", 10)+ monkeypatch.setattr(edit_diff_module, "MAX_REPLACEMENTS", 10 if len(edits) == 1 else 19)(tmp_path / "f.txt").write_text("a," * 11 + "zz\n" + "c," * 10 + "\n")result = await _edit(make_ctx, "f.txt", *edits)@@ -512,6 +600,37 @@ async def test_replace_all_has_a_budget(tmp_path, make_ctx, monkeypatch, edits,assert (tmp_path / "f.txt").read_text() == "a," * 11 + "zz\n" + "c," * 10 + "\n"+async def test_edit_work_has_a_budget_over_all_edits(tmp_path, make_ctx, monkeypatch):+ """Each edit scans the whole file, holding the GIL every Session shares: edits × size is bounded."""+ monkeypatch.setattr(edit_diff_module, "MAX_EDIT_SCAN_CHARS", 1000)+ (tmp_path / "f.txt").write_text("".join(f"line {i}\n" for i in range(100))) # 790 characters+ edits = [{"oldText": f"line {i}\n", "newText": f"LINE {i}\n"} for i in range(2)]++ one = await _edit(make_ctx, "f.txt", edits[0])+ two = await _edit(make_ctx, "f.txt", *edits)++ assert not one.is_error, result_text(one)+ assert (two.is_error, result_text(two)) == (+ True,+ "2 edits over f.txt (790 characters) are over the edit work limit. Split them into several edit calls, "+ "or use bash (for example sed or a short script).",+ )+++async def test_the_display_diff_never_splits_a_result_with_too_many_lines(tmp_path, make_ctx):+ """A result under the diff byte gate can still have a million lines; their count is checked first."""+ (tmp_path / "f.txt").write_text("t\n" + "x\n" * 100)+ tracemalloc.start()+ try:+ result = await _edit(make_ctx, "f.txt", {"oldText": "t", "newText": "ab\n" * 300_000})+ peak = tracemalloc.get_traced_memory()[1]+ finally:+ tracemalloc.stop()++ assert result.details == {"diff_skipped": True}+ assert peak < 8 * 1024 * 1024++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)))