## Summary A file an `exec` command wrote reached the desk diff as a bare row. The loop listed the working directory on either side of the command (#692), so it knew a file had changed but never what it changed from: a rewrite showed a bare `M` with no counts, and opening any such row fell back to the file viewer instead of the diff pane. File-tool edits were unaffected; this only concerned commands (`python3 gen.py > out.json`, `echo x >> notes.md`, `sed -i ...`). This supersedes #826. That PR measured the change in the loop, around a call named `exec`. Here the exec tool measures it itself, around the command it runs, and hands it back the way it already hands back the files a command removed; the loop only forwards it. How it works: - **The tool measures.** `raven/agent/tools/command_writes.py` lists the directory before and after the command and, where the checkpoint's shadow repo covers it, stages the tree first, so a rewritten or removed file is shown against what it held. The result travels on a new `ToolResult.written` / `ToolOutput.written` (`FileWrite` on the tool paper, contract version 32 -> 33, and 33 -> 34 for `FileRemoval.withheld`), beside the existing `removed`. - **The repo is handed in.** Tools do not import the loop shell (import-linter), so `ExecTool` holds the shadow repo through a `ShadowTree` protocol and a resolver the loop passes in (`AgentLoop._command_shadow`: the turn's repo, only where it covers the command's directory). The built-in is built with it, and a plugin's same-name `exec` replacement (raven-code's `CodeExecTool`) is handed it through `measure_writes` before it is registered, so it measures as the built-in does, as the loop-level listing it replaces did for any tool named `exec`. Both happen before the registry admits the tool, because the registry reads the ceiling off the spec it admits. - **Every command stages afresh inside its own call**, into a per-process index of its own in the shadow repo (never the index the per-turn commit reads), and waits up to 120 s. Whenever there is no snapshot to measure against -- the tree cannot be staged at all (checkpoint off, `working_dir` outside the repo, git error) or it did not finish within the wait -- the command runs all the same and is still listed, so its files are reported without the text they held; where a repo covers the directory, its rules still decide what text may be shown. A late staging is left running, so a later command usually finds it done. What follows the command is bounded too: the shadow repo's reads get 60 s (past it every file goes out without text) and the whole post-command step 120 s (past it the call returns the command's output with no record of its files), so a slow measurement never costs the output. `measure_writes` raises the tool's registry ceiling by what measuring may take (the 120 s wait and the 120 s bound), so the built-in's goes from 660 s to 900 s and a replacement's own ceiling keeps its margin. A git read whose caller stops waiting kills its process. - **Warm-up as a session opens.** The first staging of a directory hashes every file. `session.create` / `session.resume` call `ExecTool.warm` for the directory the session resolves to, and a session opened by its first message (a channel's) is warmed at the start of that turn. `warm` returns at once and runs on the staging thread, so it overlaps the model's first reply. - **One rule for what text may be shown.** Every file a command created, rewrote or removed, including the ones its own removal watch caught by name, carries text only where the shadow repo's rules would store it (`git check-ignore --no-index` against its excludes and the user's `.gitignore`, the rules alone, not what the index holds). So a `.env` or a key never enters the event or the stored session, even one the repo staged before an ignore rule named it, and even when the staging failed: the rules need no tree. Where no shadow repo can vouch for a file, a created file carries its counts only. - **Loop.** The exec-only listing branch in `turn_path.py` is gone; the loop turns `result.written` into `file_written`. Besides that it has the resolver above and the one-line warm-up at turn start. The registry now keeps `removed` / `written` on a result it rewrites as an error (a command whose output begins with "Error" still wrote what it wrote). A removal whose body the rules kept back goes out marked `withheld` (a new defaulted field on `FileRemoval`), and the turn's removal watch fills in a bare removal from what the turn wrote only when it is not: with the checkpoint off every bare removal stays fillable, as before, and a withheld body is never put back. - **Wire and page** (unchanged from #826). `file_written` entries gain optional `added` / `removed` / `diff`; the diffs share a 512 KiB budget per call, past which the counts still go. Contract updated in `rpc-schema/openrpc.json`, `raven/rpc/models.py` and both generated clients. ui-web draws these rows with their hunks, so they open in the diff pane with +/- in the desk list, and `fromUnified` now skips only a real `---`/`+++` header pair, so a removed `-- note` line is no longer dropped. - **Robustness.** Stagings run on daemon threads with blocking git calls (an asyncio subprocess still starting when its loop closes hangs the close on CPython 3.12 macOS). `add` and `write-tree` run under one lock per index; the staging `add` has its own 600 s ceiling; the turn commit waits for a warm-up's repo setup instead of racing it on the config lock. The setup's future and every staging future are running from creation, so a cancelled waiter cannot cancel them, and the warm-up and staging threads settle them whatever happens, so no staging is left published for nobody to finish (the cancellation 0xKT and gloryfromca measured on #826). Git queries now encode paths with `surrogateescape`, so a POSIX file name that is not UTF-8 no longer raises after the command has run (the follow-up gloryfromca raised on #826). Behaviour that moved with the measurement: staging now happens after the permission gate has ruled, so a denied command is never staged; a background command (`run_in_background`) takes no listing, since its files land after the call returns; a sub-agent's `exec` does not list (its runner lists every call itself, as before). Not covered: files the shadow repo excludes (`*.log`, `.env`, the user's `.gitignore`) get no diff, text files over 256 KB get no counts, a same-name `exec` that is not an `ExecTool` (none ships) reports no files, and existing sessions keep the rows they stored. ## Type - [x] Fix - [ ] Feature - [ ] Docs - [ ] CI / tooling - [ ] Refactor - [ ] Other ## Verification - [x] Relevant tests pass locally - [x] Relevant lint / type checks pass locally - [ ] User-facing docs or screenshots are updated when needed - `make test-python` -- 27077 passed, 108 skipped, 3 failed (on the previous head; the focused set below is on this one). The 3 failures (`test_rpc_files.py::test_a_host_without_libreoffice_says_so`, `test_simulation_scenario.py::test_a_fresh_trial_refreshes_the_subagent_homes_before_it_runs`, `test_simulation_suite.py::test_a_stopped_run_is_marked_in_its_record_and_finished_with_its_evidence`) fail the same way on `github/main` on this machine. - `uv run --frozen --all-extras pytest tests/test_runtime_checkpoint.py tests/test_runtime_checkpoint_deep.py tests/test_agent_loop_session_stamps.py tests/test_agent_loop_workdir.py tests/test_rpc_session.py tests/test_shell_command_writes.py tests/test_shell_file_removals.py tests/test_tool_registry_execute.py tests/test_tool_registry_channels.py tests/test_tool_registry_session_overlay.py tests/test_rpc_spine.py tests/test_workdir_snapshot.py tests/test_shell_approval.py tests/test_contracts_two_tier_ledger.py tests/test_kernel_budget.py tests/test_removal_watch.py tests/test_agents_code_flow_exec.py tests/test_agents_code_tools_plugin.py tests/test_agents_code_launcher.py tests/test_subagent_dag_runner.py tests/test_agents_research_ask_user_text.py tests/test_living_docs.py tests/test_tracing_compact.py -q --idle-ceiling-strict` -- 1231 passed. - `make lint-python lint-types lint-imports` -- clean (10 import contracts kept). `npm run gen:check --prefix ui-web`, `npm run type-check --prefix ui-web`, `npm run lint --prefix ui-web` (0 errors, 4 pre-existing warnings), `npm run lint:rpc --prefix ui-tui` -- clean. `npm test --prefix ui-web` -- 3107 passed. - Mutation checks, each restored afterwards and each turning at least one test red (48): a staging timeout not falling back to running the command without a diff; the wiring not asking the tool to record writes, or not handing it the repo; the resolver dropping its `covers` check, or ignoring the turn's binding; the registry dropping `written` on unwrap or on an error result; the loop ignoring `written`; no turn-start warm-up; `session.create` / `session.resume` not warming; a failing warm-up failing the open; created-file text ignoring `trackable`; the diff budget ignored; removals losing the staged text; staging a directory too large to list; reading the written files on the event loop; a named removal reported twice; the ceiling under the sum of the wait, the command cap and the measuring bound; no bound on the shadow repo's reads after the command, or on the whole post-command step; a late read keeping a named removal's text; a cancelled git read left running; the checkpoint's timeout not being a `TimeoutError`; `warm` ignoring `record_writes`; a background command measured; the setup future not marked running; the warm-up or a command's staging not settling its future on an unexpected error; a rewritten or removed file read without the rule; a named removal's text kept without the rule; the repo dropped when the staging failed, or its rules applied only where there is a tree; a tree-less staging still read for blobs; the removal watch refilling a withheld body, or never refilling an unknown one; a named, listed or timed-out removal not marked withheld where the repo kept its body back, or a listed one marked where no repo ruled; measuring switched on only for the built-in, so a plugin's same-name `exec` reports nothing; `measure_writes` not raising the ceiling, or raising it on every call; the built-in or the replacement measured only after the registry admitted it, so the admitted ceiling stays at 660 s; a failed staging not warned about, or warned about on every command. - Real gateway (`raven serve` from this branch, isolated `RAVEN_HOME`, real model), in a cold 12,775-file / 428 MB directory on a machine running endpoint protection at load average 13-14. The first message opened the session and the warm-up started with it; the model's first `exec` (append to a `notes.md` Raven had never touched, and create `made_by_exec.txt`) waited out the cold staging and took 37.8 s in total. The second `exec` took 3.7 s, with the staging under the 1 s sampling interval. The desk lists `M notes.md +2` and `A made_by_exec.txt +3`; `notes.md` opens in the diff pane with both commands' hunks, and a reload draws the same rows. ## Risk - [x] Security impact considered - [x] Backward compatibility considered - [x] Rollback path is clear for risky changes User-visible changes: - An `exec` now runs only after the working directory has been staged (or the 120 s wait has passed). On a warm directory that is well under a second; the first command in a large directory the shadow repo has never indexed waits for the first staging (tens of seconds on a slow disk), which the session-open warm-up usually hides. Past 120 s the command runs without a diff. - Opening a session starts one background `git add` per directory per process, writing into the same `.raven/shadow.git` the checkpoint already uses. Each process keeps its own `exec-<pid>.index` there; files older than 7 days are pruned. - No file content is stored beyond what the checkpoint already commits, and the same excludes apply. With `runtime.checkpoint.policy = "never"` a command's rewrite is reported bare, as today, and a created file carries counts only. - The new `FileWritten` fields are optional, so older clients ignore them. `ToolResult` / `ToolOutput` gain a defaulted `written` field and `FileRemoval` a defaulted `withheld`; contract version 34. Rollback: revert the commit. The staging indexes in `.raven/shadow.git` are inert without the code and are pruned by age. ## Related Issues Supersedes #826 --------- Co-authored-by: arelchan <204152633+arelchan@users.noreply.github.com> Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
354 lines
15 KiB
Python
354 lines
15 KiB
Python
"""The exec tool's record of what a command wrote: its directory either side.
|
|
|
|
A command returns its output and nothing else, so the tool lists the directory
|
|
it runs in before and after, and -- where a shadow repo covers the directory --
|
|
stages the tree first so a rewritten or removed file can be shown against what
|
|
it held. The shadow repo here is a stand-in: these are the tool's decisions
|
|
(when to stage, when not to run, what to hand back), and the real repo has its
|
|
own tests in ``test_runtime_checkpoint.py``.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import threading
|
|
from pathlib import Path
|
|
from typing import Any, Collection
|
|
|
|
import pytest
|
|
|
|
from raven.agent.loop.checkpoint import StagingTimeoutError
|
|
from raven.agent.tools import command_writes, snapshot
|
|
from raven.agent.tools.shell import ExecTool
|
|
|
|
|
|
class _Shadow:
|
|
"""A shadow repo whose tree is whatever the directory held at staging."""
|
|
|
|
def __init__(
|
|
self, root: Path, *, stage: Any = None, ignored: Collection[str] = (), answer_after: float = 0.0
|
|
) -> None:
|
|
self._root = root
|
|
self._stage = stage
|
|
self._answer_after = answer_after
|
|
self._ignored = set(ignored)
|
|
self._trees: dict[str, dict[str, bytes]] = {}
|
|
self.staged = 0
|
|
self.warmed = 0
|
|
|
|
async def warm(self) -> None:
|
|
self.warmed += 1
|
|
|
|
async def stage_tree(self) -> str | None:
|
|
self.staged += 1
|
|
if self._stage is not None:
|
|
return self._stage()
|
|
tree = f"t{self.staged}"
|
|
self._trees[tree] = {str(p): p.read_bytes() for p in self._root.rglob("*") if p.is_file()}
|
|
return tree
|
|
|
|
async def read_blobs(self, tree: str, paths: Collection[str], *, max_bytes: int) -> dict[str, bytes]:
|
|
assert tree is not None, "no tree was staged, so there is none to read"
|
|
held = self._trees.get(tree, {})
|
|
return {path: held[path] for path in paths if path in held and len(held[path]) <= max_bytes}
|
|
|
|
async def trackable(self, paths: Collection[str]) -> set[str]:
|
|
await asyncio.sleep(self._answer_after)
|
|
return {path for path in paths if Path(path).name not in self._ignored}
|
|
|
|
|
|
def _tool(root: Path, shadow: _Shadow | None = None, *, record_writes: bool = True) -> ExecTool:
|
|
return ExecTool(
|
|
working_dir=str(root),
|
|
restrict_to_workspace=True,
|
|
record_writes=record_writes,
|
|
shadow=(lambda _root: shadow) if shadow is not None else None,
|
|
)
|
|
|
|
|
|
async def test_a_rewrite_is_measured_against_the_tree_staged_just_before_it(tmp_path):
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
shadow = _Shadow(tmp_path)
|
|
|
|
result = await _tool(tmp_path, shadow).execute(command="echo two >> notes.md")
|
|
|
|
assert shadow.staged == 1
|
|
[write] = result.written
|
|
assert (write.path, write.created, write.added, write.removed) == (str(tmp_path / "notes.md"), False, 1, 0)
|
|
assert "+two" in write.diff.splitlines()
|
|
|
|
|
|
@pytest.mark.parametrize("error", [TimeoutError, StagingTimeoutError])
|
|
async def test_a_command_whose_tree_is_not_staged_in_time_still_runs_without_a_diff(tmp_path, error):
|
|
"""The command matters more than its diff. Past the wait it runs all the
|
|
same and is still listed, so the file it rewrote is reported -- only not
|
|
what that file held. The checkpoint's own timeout must be a ``TimeoutError``
|
|
for this: the tool holds the repo through a protocol and cannot import it."""
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
|
|
def _too_slow() -> str:
|
|
raise error
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, stage=_too_slow)).execute(command="echo two >> notes.md")
|
|
|
|
assert (tmp_path / "notes.md").read_text() == "one\ntwo\n", "the command must have run"
|
|
assert result.ok is True
|
|
[write] = result.written
|
|
assert (write.created, write.added, write.diff) == (False, None, None)
|
|
|
|
|
|
async def test_a_tree_that_cannot_be_staged_leaves_the_command_to_run_without_what_its_files_held(tmp_path):
|
|
"""A git that failed, a directory the repo cannot hold: nothing about the
|
|
filesystem says the command should not run, so it does. A rewrite has no
|
|
earlier text to be shown against; a created file needs none."""
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, stage=lambda: None)).execute(
|
|
command="printf 'x\\n' > made.txt && echo two >> notes.md"
|
|
)
|
|
|
|
assert (tmp_path / "made.txt").read_text() == "x\n"
|
|
written = {Path(w.path).name: w for w in result.written}
|
|
assert (written["made.txt"].created, written["made.txt"].lines, written["made.txt"].added) == (True, 1, 1)
|
|
assert "+x" in written["made.txt"].diff.splitlines()
|
|
assert (written["notes.md"].added, written["notes.md"].diff) == (None, None)
|
|
|
|
|
|
def _no_tree() -> None:
|
|
return None
|
|
|
|
|
|
def _late_tree() -> str:
|
|
raise TimeoutError
|
|
|
|
|
|
@pytest.mark.parametrize("stage", [_no_tree, _late_tree], ids=["not-staged", "staged-too-late"])
|
|
async def test_a_tree_that_was_not_staged_still_leaves_the_repo_to_rule_on_what_may_be_shown(tmp_path, stage):
|
|
"""Whether a file's text may be shown is the repo's rules, which need no
|
|
tree. A staging that failed or ran late costs the diffs a tree would give;
|
|
it must not also let a ``.env`` the command removed by name, or created,
|
|
go out with its text, while an ordinary file keeps its own."""
|
|
(tmp_path / ".env").write_text("API_KEY=top-secret\n")
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, stage=stage, ignored={".env", "made.env"})).execute(
|
|
command="rm .env notes.md && printf 'TOKEN=x\\n' > made.env"
|
|
)
|
|
|
|
assert {Path(r.path).name: r.before for r in result.removed} == {".env": None, "notes.md": "one\n"}
|
|
[write] = result.written
|
|
assert (Path(write.path).name, write.added, write.diff) == ("made.env", 1, None)
|
|
|
|
|
|
async def test_a_created_file_the_repo_would_not_store_keeps_its_counts_and_loses_its_text(tmp_path):
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, ignored={".env"})).execute(
|
|
command="printf 'API_KEY=top-secret\\n' > .env && printf 'ok\\n' > notes.md"
|
|
)
|
|
|
|
written = {Path(w.path).name: w for w in result.written}
|
|
assert written[".env"].diff is None and written[".env"].added == 1
|
|
assert "+ok" in written["notes.md"].diff.splitlines()
|
|
|
|
|
|
async def test_a_removed_file_carries_what_the_staged_tree_held(tmp_path):
|
|
(tmp_path / "doomed.txt").write_text("one\ntwo\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path)).execute(command="find . -name '*.txt' -delete")
|
|
|
|
assert [(r.path, r.before) for r in result.removed] == [(str(tmp_path / "doomed.txt"), "one\ntwo\n")]
|
|
|
|
|
|
async def test_a_file_the_command_named_keeps_its_text_only_where_the_repo_would_store_it(tmp_path):
|
|
"""The command's own watch reads a named file before it goes. That text is
|
|
held to the same rule as every other file's: a ``.env`` removed by name goes
|
|
out without its body, an ordinary file with it."""
|
|
(tmp_path / ".env").write_text("API_KEY=top-secret\n")
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, ignored={".env"})).execute(command="rm .env notes.md")
|
|
|
|
assert {Path(r.path).name: (r.before, r.withheld) for r in result.removed} == {
|
|
".env": (None, True),
|
|
"notes.md": ("one\n", False),
|
|
}
|
|
|
|
|
|
@pytest.mark.parametrize("shadowed", [True, False])
|
|
async def test_a_listed_removal_says_whether_its_body_was_kept_back_or_is_unknown(tmp_path, shadowed):
|
|
"""A bare removal is either one the rules kept back, which nothing else may
|
|
fill in, or one whose text nobody had, which the turn's own record of the
|
|
file may. Only a repo that ruled can say the first."""
|
|
(tmp_path / "keys.secret").write_text("TOKEN=x\n")
|
|
shadow = _Shadow(tmp_path, stage=lambda: None, ignored={"keys.secret"}) if shadowed else None
|
|
|
|
result = await _tool(tmp_path, shadow).execute(command="find . -name '*.secret' -delete")
|
|
|
|
[removal] = result.removed
|
|
assert (removal.before, removal.withheld) == (None, shadowed)
|
|
|
|
|
|
async def test_a_tool_not_asked_to_record_writes_does_not_list_or_stage(tmp_path, monkeypatch):
|
|
"""A sub-agent's runner lists every call itself, so its ``exec`` does not
|
|
walk the directory a second time around each command."""
|
|
roots: list[Any] = []
|
|
monkeypatch.setattr(snapshot, "take", lambda root: roots.append(root))
|
|
shadow = _Shadow(tmp_path)
|
|
|
|
result = await _tool(tmp_path, shadow, record_writes=False).execute(command="echo x > made.txt")
|
|
|
|
assert roots == [] and shadow.staged == 0
|
|
assert result.written == ()
|
|
|
|
|
|
async def test_a_background_command_takes_no_listing(tmp_path, monkeypatch):
|
|
"""Its files land after the call has returned, so a listing either side of
|
|
the start would describe nothing it did."""
|
|
roots: list[Any] = []
|
|
monkeypatch.setattr(snapshot, "take", lambda root: roots.append(root))
|
|
|
|
await _tool(tmp_path, _Shadow(tmp_path)).execute(command="true", run_in_background=True)
|
|
|
|
assert roots == []
|
|
|
|
|
|
async def test_a_directory_too_large_to_list_is_not_staged(tmp_path, monkeypatch):
|
|
"""No listing means nothing to report, so a staging would be paid for nothing."""
|
|
monkeypatch.setattr(snapshot, "take", lambda root: None)
|
|
shadow = _Shadow(tmp_path)
|
|
|
|
result = await _tool(tmp_path, shadow).execute(command="echo x > made.txt")
|
|
|
|
assert shadow.staged == 0
|
|
assert result.written == ()
|
|
|
|
|
|
async def test_reading_the_written_files_never_runs_on_the_event_loop(tmp_path, monkeypatch):
|
|
"""This reads every written file whole, and one command can write a
|
|
hundred; every other session on the process waits behind the loop."""
|
|
real = command_writes._writes
|
|
threads: list[int] = []
|
|
|
|
def watched(*args: Any, **kwargs: Any) -> Any:
|
|
threads.append(threading.get_ident())
|
|
return real(*args, **kwargs)
|
|
|
|
monkeypatch.setattr(command_writes, "_writes", watched)
|
|
|
|
result = await _tool(tmp_path).execute(command="echo x > made.txt")
|
|
|
|
assert result.written
|
|
assert threads and threading.get_ident() not in threads
|
|
|
|
|
|
async def test_past_the_calls_diff_budget_the_counts_still_go_and_the_diff_does_not(tmp_path, monkeypatch):
|
|
"""One command can rewrite a hundred files. The diffs share the call's
|
|
budget, and one past it is dropped whole -- half a diff reads as a smaller
|
|
change -- while its counts, which cost nothing, still say how big it was."""
|
|
monkeypatch.setattr(command_writes, "DIFF_BUDGET_CHARS", 60)
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path)).execute(
|
|
command="printf 'one\\ntwo\\n' > a.txt && printf 'three\\nfour\\n' > b.txt"
|
|
)
|
|
|
|
first, second = sorted(result.written, key=lambda w: w.path)
|
|
assert first.diff is not None and second.diff is None
|
|
assert (second.added, second.removed) == (2, 0)
|
|
|
|
|
|
async def test_warming_stages_through_the_shadow_repo_and_is_a_no_op_without_one(tmp_path):
|
|
shadow = _Shadow(tmp_path)
|
|
|
|
await _tool(tmp_path, shadow).warm(tmp_path)
|
|
await _tool(tmp_path).warm(tmp_path)
|
|
await _tool(tmp_path, shadow, record_writes=False).warm(tmp_path)
|
|
|
|
assert shadow.warmed == 1, "only the tool that records writes warms for them"
|
|
|
|
|
|
def test_a_measuring_tools_ceiling_covers_the_longest_command_and_the_longest_waits():
|
|
"""The registry kills a call past the tool's ceiling. A measured command may
|
|
wait out its staging, run to the executor's own cap and then have its files
|
|
measured, and a ceiling under the three together would kill a command the
|
|
executor was still allowed to run -- losing its output. Asked twice, the
|
|
tool raises its ceiling once."""
|
|
from raven.agent.loop import checkpoint
|
|
|
|
tool = ExecTool(record_writes=True)
|
|
spent = checkpoint._STAGE_WAIT_SECONDS + ExecTool._MAX_TIMEOUT + command_writes.AFTER_WAIT_SECONDS
|
|
assert tool.timeout_seconds > spent
|
|
assert command_writes.MEASURE_SECONDS == checkpoint._STAGE_WAIT_SECONDS + command_writes.AFTER_WAIT_SECONDS
|
|
tool.measure_writes(None)
|
|
assert tool.timeout_seconds == ExecTool.timeout_seconds + command_writes.MEASURE_SECONDS
|
|
assert ExecTool().timeout_seconds == ExecTool.timeout_seconds > ExecTool._MAX_TIMEOUT
|
|
assert command_writes.AFTER_WAIT_SECONDS > command_writes.READ_WAIT_SECONDS
|
|
|
|
|
|
def test_a_calls_diffs_share_the_budget_its_file_changes_and_removed_bodies_do():
|
|
"""One event budget, written in two places: the tool cannot import the
|
|
loop's constant, so this holds them equal."""
|
|
from raven.agent.loop import _shared
|
|
|
|
assert command_writes.DIFF_BUDGET_CHARS == _shared._FILE_CHANGE_MAX_CHARS
|
|
|
|
|
|
async def test_a_shadow_repo_that_cannot_stage_says_so_once_per_directory(tmp_path, monkeypatch):
|
|
"""A broken repo measures every command without diffs, and the reason
|
|
must reach the log above debug -- once, not on every command."""
|
|
from loguru import logger
|
|
|
|
monkeypatch.setattr(command_writes, "_UNSTAGED", set())
|
|
seen: list[str] = []
|
|
sink = logger.add(lambda message: seen.append(str(message)), level="WARNING")
|
|
try:
|
|
tool = _tool(tmp_path, _Shadow(tmp_path, stage=lambda: None))
|
|
await tool.execute(command="echo one > a.txt")
|
|
await tool.execute(command="echo two > b.txt")
|
|
finally:
|
|
logger.remove(sink)
|
|
|
|
assert sum("could not stage" in line for line in seen) == 1
|
|
|
|
|
|
async def test_a_shadow_repo_too_slow_to_answer_after_the_command_leaves_every_file_bare(tmp_path, monkeypatch):
|
|
"""The command has run; what the repo would have said about its files is
|
|
worth less than its output. Past the wait the files go out without text --
|
|
including a named removal's, which the repo never got to rule on."""
|
|
monkeypatch.setattr(command_writes, "READ_WAIT_SECONDS", 0.05)
|
|
(tmp_path / "notes.md").write_text("one\n")
|
|
(tmp_path / "gone.txt").write_text("secret\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path, answer_after=5.0)).execute(
|
|
command="echo two >> notes.md && rm gone.txt"
|
|
)
|
|
|
|
assert "Exit code: 0" in result
|
|
[write] = result.written
|
|
assert (write.added, write.diff) == (None, None)
|
|
assert [(Path(r.path).name, r.before, r.withheld) for r in result.removed] == [("gone.txt", None, True)]
|
|
|
|
|
|
async def test_measuring_that_runs_past_its_bound_still_returns_the_commands_output(tmp_path, monkeypatch):
|
|
"""Everything after the command is bounded short of the registry's ceiling,
|
|
so a slow walk costs the record of the files and never the output."""
|
|
monkeypatch.setattr(command_writes, "AFTER_WAIT_SECONDS", 0.2)
|
|
real_take = snapshot.take
|
|
walks: list[int] = []
|
|
|
|
def _slow_second_walk(root: Any) -> Any:
|
|
walks.append(1)
|
|
if len(walks) == 2:
|
|
import time
|
|
|
|
time.sleep(1.0)
|
|
return real_take(root)
|
|
|
|
monkeypatch.setattr(snapshot, "take", _slow_second_walk)
|
|
|
|
(tmp_path / "gone.txt").write_text("secret\n")
|
|
|
|
result = await _tool(tmp_path, _Shadow(tmp_path)).execute(command="echo done && echo x > made.txt && rm gone.txt")
|
|
|
|
assert "done" in result and result.ok is True
|
|
assert result.written == ()
|
|
assert [(Path(r.path).name, r.before, r.withheld) for r in result.removed] == [("gone.txt", None, True)]
|