Raven-1/tests/test_ops_remote_path.py
Handsome-wzw aa023212a0
feat(*): pick up a turn left on a registered machine when this computer stops (#934)
## Summary

Registered machines are now available without a debug environment
switch.
`RAVEN_DEBUG_MACHINES` previously defaulted to off: it kept
machine-backed
agents out of the dispatch table, refused node/installer access, hid
remote
preset destinations, and treated file paths locally while off. It was a
trial feature gate, not a verbose-logging option. At the owner's
request,
this PR removes the variable and all of those checks. Normal startup and
restart recovery use the configured machine entries; even a leftover
value
of `0` is ignored. Machine registration, SSH authentication, file
permission
prompts, and concurrency limits still apply.

Fourth change in the series that lets a Raven dispatch work to the
machines in its machine registry. #920, #921 (merged through #922) and
#922 are on main; this change stands on main alone now, and the diff is
its own.

What this does: a turn running on a registered machine survives this
computer stopping, and the next start picks it up and reports it.

- While a turn runs on a machine, this computer remembers what the
  machine does not know (`raven/acp_client/remote_runs.py`): one
  owner-only file per run under `<raven home>/machines/runs/`, with the
machine's registry id, the task id its daemon runs the agent under, the
  agent, the session key, the instance handle, the dispatch id, the ACP
  session, the id of the prompt whose answer is awaited, the ids this
  side has used, how many of the agent's frames were read here, the
  agent's answer to `initialize` and the connection's launch key. Kept
  current after every frame the connection has dealt with, removed when
  the turn ends here. No address is in it.
- When this process stops -- the gateway, `serve`, the TUI, the page
  stack -- the
  agents on machines stay with their daemons
  (`remote.leave_agents_running()` before the drain): a cancelled turn
on a machine sends no `session/cancel`, closing its connection ends the
endpoint only, and the manager leaves the dispatch's record open rather
  than finishing it as cancelled. A hard kill or `/restart` leaves the
  same record, written as the turn ran.
- At the next start -- the gateway once its generation is bound; the
`serve`, TUI and page stacks once --
`raven/agent/subagent/resume_remote.py`
picks every run up: `task/list` on the machine, `agent/attach` from the
  frame last read (`NodePool.attach`), the connection adopted into the
  pool with request ids carried on past the other process's
  (`AcpConnectionPool.adopt`, `AcpClient.continue_ids_from`), the
  prompt's answer awaited (`AcpClient.await_response`), the reply built
  as a turn ended here is -- the partial notice when the agent stopped
  early, the machine note saying it was picked up after this computer
  restarted -- then the dispatch's record finished, the registry row and
  the node claim closed, and the result announced into the conversation
  that asked, as the manager announces a turn that ended here
  (`raven/acp_client/remote_resume.py`). A machine that cannot be asked
  keeps its record for a later start; a run the machine no longer has,
  one whose agent ended while this computer was away, one whose agent
  left the agent table, and one whose turn says nothing are announced as
  errors that say so. The adopted agent process serves later dispatches
  of its entry, as one started here would.
- Seams: `AcpClient.request(sent=)` hears the id of what goes out;
  `ChannelEndpoint.watchers` hear the frames the connection has dealt
  with (`consumed`, called by the read loop); `ChannelEndpoint.stays`
  reads the leaving flag.
- A cli entry's run on a machine is recorded while it runs (kind `cli`,
  no ACP part) and left the same way, the manager reading the dispatch
  as left; the entry picks it up (`CliAgentBackend.pick_up`): the daemon
  replays the command's whole output from its first line, the exit code
  ends it, and the reply closes with the machine note.
- The record carries the task as the model wrote it, and -- once the
  manager has read the run as left -- the dispatch's origin and summary
  (`remote_runs.annotate`): the announce after a restart names the
  instance, node and tool call as the announce here does and quotes the
  task as authored, never the rendered prompt on disk; a host killed
  outright gets an origin made up from the spawn record and the run.
- The record goes last, once the outcome has reached its conversation:
  a conclusion that fails, a stop under it, or an announce the scheduler
  refuses (`_announce_result` says so now) leaves the record, its claim
  released, for the next start to try again.
- A run that returned here keeps its record the same way, until the
  manager's announce of the reply was taken: the manager dispatches with
  `reported_by_caller=True`, the backend then leaves the record behind
  with the reply instead of removing it as it returns, and the manager
  removes it after the announce (`remote_runs.forget_dispatch`) -- or,
  when the draining scheduler refused the announce, annotates it with
  the dispatch's origin and summary for the next start to report. A host
  killed between the agent's answer and the announce therefore still has
  the way back to the run. A caller that takes the reply as its own
  result (a graph node, a probe, a direct chat) dispatches without the
  keyword, and the record goes as it did. The record's frame count stops
  at the prompt: the turn's own frames, the chunks before the answer as
  much as the answer, are never checkpointed, so a pick-up replays the
  turn whole and the reply it reports is complete; a record that had
  moved past the answer would have resumed after it and waited forever.

Not in this change: a run from the DAG lane has no spawn record to
finish; it is announced from the origin the manager added to its record
when it was left, or, for a host killed outright, from the run's record
alone. The Tasks page reads the finished record on its next load rather
than live.

## Type

- [ ] Fix
- [x] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other

## Verification

Machine availability and restart checks after removing the trial switch:

- New default-behavior regressions were run before the removal: 8
failed,
  with the enabled-value control passing. After removal, all 9 passed,
  including ACP/CLI recovery through the real AgentRegistry with the
  variable absent or set to `0`.
- `uv run --no-sync pytest tests/test_acp_remote.py
tests/test_node_client.py tests/test_node_server.py
tests/test_node_install.py tests/test_ops_remote_path.py
tests/test_permissions_remote_read.py tests/test_remote_file_tools.py
tests/test_deliver_files_tool.py tests/test_raven_config_tool.py
tests/test_rpc_subagents.py tests/test_shell_machine_channel.py
tests/test_subagent_acp_remote.py tests/test_subagent_cli_remote.py
tests/test_resume_remote.py tests/test_remote_resume.py
tests/test_remote_runs.py tests/test_subagent_manager.py
tests/test_cli_tui_commands.py tests/test_rpc_schema_match.py
tests/test_config_schema.py -n 4 --idle-ceiling-strict --cov=raven
--cov-branch --cov-report=json:<coverage-json> --cov-report= --tb=short`
  -- 1799 passed on the combined tree, including the latest published
result-retention fix. An earlier run had a missing stdio response in the
minimal-node dependency test and an idle-gate hit; both passed a focused
recheck, and this final complete selection passed with the strict gate.
- `uv run --no-sync python scripts/coverage_gate.py --coverage-json
<coverage-json> diff --base-ref origin/main --threshold 90`
-- 92.92% (656/706 changed executable lines) for this PR; the
gate-removal
commit alone covers 2/2 new executable lines (the remainder deletes
gates
  and updates descriptions).
- `UV_NO_SYNC=1 make lint-python lint-imports lint-deps lint-types`, the
commit/source-language/large-file scope gates and `git diff --check`
pass.
Both RPC generators' `--check` runs pass; `npm test --
scripts/gates/fixture-shape.test.mjs`
  from `ui-web` passes all five tests.
- Real Linux machine, isolated node installations, separate dispatch and
restart processes: before removal, a task started with the trial switch
on was reported as an absent agent and its recovery record deleted when
  restarted without it. After removal, both absent and old `0` settings
  recover the full CLI result and read its output file by address. The
absent-variable case kills the relay before host shutdown. The combined
  final tree was reinstalled and both scenarios repeated successfully.
- With the variable absent, 12 further timing/boundary probes pass:
early
  CLI exit and blank lines, relay loss before heartbeat with normal-call
recovery, tunnel loss, ACP/CLI/OpenAI cancellation during disconnection,
  11 launches admitting 8 and refusing 3, occupied tunnel port, and an
unwritable prompt file. Three attachment, POSIX-command and IPv6 checks
  also pass; Windows command parsing is simulated, not native Windows.
No test task or tunnel remains; the dedicated node directories are
removed.
- CONTEXT.md, configuration and RPC descriptions, transport/path
docstrings,
  both collaboration guides and the owner's design notes were checked
against the code. The guides explain the former trial switch and normal
startup without it. The existing SSH, permission and limit tests
continue
  to exercise their checks with no test-only switch setup.

- `env -u RAVEN_DEBUG_MACHINES uv run --no-sync pytest -n 4 --tb=short
--cov=raven --cov-branch --cov-report=json:<full-coverage-json>
--cov-report=`
-- 31345 passed, 230 skipped, 2 xfailed, 5 failed. A separate
current-main
  checkout reproduces four failures: CLI color rendering on this host,
LibreOffice detection, and the two simulation cases. The remaining
failure
  was the direct-chat test stub rejecting `reported_by_caller` from the
latest result-retention change; the published wrapper fix now forwards
only keywords declared by its fake backend. The full
suite was not rerun for that test-only correction. Final direct-chat and
manager recheck: 277 passed with strict idle accounting. Full-PR
changed-line coverage from this
  run is 96.18% (679/706).

Earlier verification:

- `uv run pytest tests/test_remote_runs.py tests/test_remote_resume.py
tests/test_resume_remote.py tests/test_acp_client_resume.py
tests/test_node_client.py tests/test_subagent_acp_remote.py
tests/test_subagent_cli_remote.py tests/test_cli_tui_commands.py
tests/test_subagent_manager.py tests/test_rpc_bootstrap.py
tests/test_cli_gateway_commands.py tests/test_acp_client_launch.py
tests/test_subagent_acp.py tests/test_scope_canon.py
tests/test_l4_entrances.py`
-- all passed (605 passed). Covered: the record written owner-only, kept
current
per frame and per prompt, removed when the turn ends here and kept when
  this process leaves; a dispatch left at shutdown neither finished nor
  announced by the manager; an endpoint ended while leaving leaves the
  agent with the daemon (real daemon); a task picked up from the frame
  last read, with the watchers hearing each frame; a pick-up the machine
  refuses or cannot carry; an answer awaited for a request another
  process sent, ids carried on, no `session/cancel` while leaving; the
  reply read off replayed frames with the other sessions' frames
  ignored, a turn cut short marked partial, an unreachable machine
  keeping the record, a run the machine no longer has, a prompt never
  sent, an agent that ended while this computer was away, an answer
  waited out, a turn that said nothing, a pick-up beside a live
  connection closing its own; the manager-side conclusion: record
  finished and announced, an error announced and the record failed, an
  unreachable machine keeping the record, an agent off the table, a
  failure inside one pick-up not stopping the next, a run with no
  conversation, the once-per-process scheduling; a cli run recorded
  while it runs and left at shutdown, picked up whole on the real daemon
  behind the stand-in ssh, its pick-up's errors said (a non-zero exit, a
  machine that no longer has it, a link down, this computer leaving), a
  cancel that is not a leave ending the command with no record left; the
  TUI's stop saying the agents stay before the cancel and its start
  scheduling the pick-up; the record annotated with the dispatch's
  origin and summary when the manager reads it as left, the announce
  quoting the task as authored and naming instance, node and tool call,
  with and without the manager's fields; the record outliving a refused
  announce and a failed conclusion, claim released; a cli record read
  without an ACP session, an older record read with defaults; the record
  moving with the frames before the prompt and not past it, a dispatch's
  record forgotten by the manager's id with its claim, the manager
  dispatching with `reported_by_caller` and forgetting the record after
  an announce that was taken and annotating it after one that was
  refused; on the real daemon behind the stand-in ssh, an acp reply and
  a cli output returned with the record kept and the host "stopped"
  before the announce, the next start reading the same reply again
whole, and a dispatch without the keyword leaving no record. End to end
on the real
  daemon behind the stand-in ssh: a turn dispatched, this process
  "stopped" the way the gateway stops, the agent still running on the
  daemon, then a fresh pick-up reading its answer, finishing the record
  and announcing it, with the agent's process the pool's connection
  again.
- `uv run pytest` on this tree, at an earlier revision of this change --
29270 passed, 121 skipped, 3 failed:
  `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`
and
`test_simulation_suite.py::test_a_stopped_run_is_marked_in_its_record_and_finished_with_its_evidence`,
  which fail the same way on main on this machine; diff coverage of this
  change against its base: 97.45% (153 of 157 executable changed lines),
  and 94.67% for the four changes against main; `lint-imports` 10 kept,
  0 broken; `ty` clean.
- Real machine (a registered Linux machine with Claude Code signed in,
  node reinstalled with this tree): process A dispatched a 25 s task,
  saw the prompt out after 2.4 s and the record keep up with the frames
  (6, then 10), then stopped the way the gateway stops 5.4 s after the
  dispatch, leaving the record and the agent running there; process B,
  started fresh, found the record and the running task, picked it up
  in 2.4 s, announced the agent's reply to the conversation with the
  machine note, finished the record, removed the run's record, held the
  agent's process as the pool's connection, and closing the pool ended
  the agent on the machine. This revision, the cli lane with the code
  before and after, the node reinstalled for each: a cli entry
  `sh -c 'echo $$ > pid.txt; sleep 20; echo done; cat'` was dispatched
  and the host stopped the gateway's way 2.7 s later. Before: no
  record, the command left running on the machine, and the next start
  found nothing to pick up. After: the record kept with the task as
  authored and the manager's origin, the command running on, and the
  next start picked it up in 16.8 s (the sleep), announcing `done` and
  `hi` with the instance, node and tool call and the machine note. The
  same with the link killed before the stop: recorded, left, picked up.
  With the machine's daemon ended between the stop and the pick-up, it
  ended its groups on its way out, the task's record there said exit
  -9, and the pick-up announced that as the run's error in 0.1 s,
  leaving nothing on the machine.
- `scripts/check_source_language.py`, `scripts/check_commit_messages.py`
  and `scripts/check_large_files.py` on the branch range, `commitlint`,
  `ruff` -- all clean.

- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [x] User-facing docs or screenshots are updated when needed

The English and Chinese agent-collaboration guides explain normal
startup,
registered-machine setup and recovery, and the removed trial switch.

## Risk

Configured machine entries are available by default; entries without a
`machine` remain local. A remote-looking file path is resolved against
the
registry even without an environment variable; use an explicit local
path
such as `./a:b` for a local filename containing a colon. A formerly
hidden
machine entry is now available for dispatch, and an old variable set to
`0`
no longer disables it. Stopping Raven no longer cancels a turn
running on a machine; it goes on there until the agent finishes or is
cancelled, and its answer arrives in the conversation after the next
start, as a sub-agent result that triggers a turn. One record per run in
the raven home, owner-only, removed when the run is concluded; a machine
that cannot be reached keeps it, so a run is reported at most once and
never lost quietly. The adopted connection answers requests with ids
past the ones the stopped process used, so a replayed frame resolves
nothing sent after the restart. Rollback is a plain revert; a record
left
behind is then an inert file.

- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes

## Related Issues

#920 and #922 (merged); #921 was carried by #922.

---------

Co-authored-by: Claude (claude-fable-5-1) <noreply@anthropic.com>
Co-authored-by: Codex <noreply@openai.com>
2026-10-14 21:34:19 +08:00

367 lines
14 KiB
Python

"""raven.ops.remote_path: a file on a registered machine, written the way scp writes one.
The rules are scp(1)'s own (OpenSSH 9.9p2): the short form has no port field,
the URI form is the only one with a port, and in it the first slash after the
host only separates. A path names exactly one registered machine -- by its id,
or by address and port -- or it is refused before any ssh. Shown, it uses the
registry's user, address and port, in a form scp reads back.
"""
from __future__ import annotations
import json
import re
from pathlib import Path
from types import SimpleNamespace
import pytest
from raven.ops import remote_path
from raven.ops.remote_path import RemotePathError, absolute, looks_remote, parse, within
ROWS = [
{"id": "box2", "display_name": "Box 2", "host": "203.0.113.7", "port": 49273, "user": "root"},
{"id": "box", "display_name": "Box", "host": "203.0.113.7", "port": 41941, "user": "root"},
{"id": "std", "host": "198.51.100.9", "port": 22, "user": "alice", "paths": ["/data/shared"]},
{"id": "v6", "host": "2001:db8::5", "port": 22, "user": "root"},
]
@pytest.fixture(autouse=True)
def _fresh_state(monkeypatch):
monkeypatch.setattr(remote_path, "_HOMES", {})
monkeypatch.setattr(remote_path, "_NODES", None)
def _where(text: str):
found = parse(text, ROWS)
assert found is not None, text
return found.machine, found.path
# --- the two forms ------------------------------------------------------------------
@pytest.mark.parametrize(
("text", "machine", "path"),
[
# The machine's id, the id with its user, and the URI with the address.
("box2:/root/x", "box2", "/root/x"),
("root@box2:/root/x", "box2", "/root/x"),
("scp://root@203.0.113.7:49273//root/x", "box2", "/root/x"),
# The first slash after host:port only separates.
("scp://203.0.113.7:49273/root/x", "box2", "root/x"),
("scp://box2", "box2", ""),
# After the colon, a path not starting with / is under the home.
("box2:", "box2", ""),
("box2:raven-work/a", "box2", "raven-work/a"),
("box2:~/raven-work/a", "box2", "~/raven-work/a"),
# An address with no port is port 22; the registry decides the rest.
("alice@198.51.100.9:/data/x", "std", "/data/x"),
("198.51.100.9:/data/x", "std", "/data/x"),
("sftp://alice@198.51.100.9//data", "std", "/data"),
("root@[2001:db8::5]:/etc/hostname", "v6", "/etc/hostname"),
("scp://root@[2001:db8::5]:22//etc/hostname", "v6", "/etc/hostname"),
# An address is matched without regard to case; a user and an id are not.
("scp://root@203.0.113.7:49273//x", "box2", "/x"),
],
)
def test_a_remote_path_names_one_registered_machine(text, machine, path):
assert _where(text) == (machine, path)
def test_a_user_and_a_machine_id_are_compared_as_written():
with pytest.raises(RemotePathError, match="registered for another user"):
parse("scp://ROOT@203.0.113.7:49273//x", ROWS)
with pytest.raises(RemotePathError, match="no registered machine matches it"):
parse("BOX2:/x", ROWS)
@pytest.mark.parametrize(
("text", "said"),
[
# No port means 22, and no registered machine is on 22 there.
("root@203.0.113.7:/etc/hostname", "no registered machine matches it"),
# The short form has no port field -- `49273:/a` is a file name on 22.
("root@203.0.113.7:49273:/a", "no registered machine matches it"),
("bob@std:/x", "logs in as 'bob', and machine 'std' is registered for another user"),
("scp://box2:41941//x", "names machine 'Box 2' (box2) on a port it is not registered on"),
("scp://203.0.113.7:99999//x", "names port '99999', which is not a port"),
("scp://203.0.113.7:ssh//x", "names port 'ssh', which is not a port"),
("[2001:db8::5/x", "opens an address with '[' and does not close it"),
("root@[2001:db8::5:/x", "opens an address with '['"),
("scp://[2001:db8::5//x", "opens an address with '['"),
("scp://[2001:db8::5]x//x", "something after the address that is not a port"),
("nobox:/x", "write it as ./name or as an absolute path"),
# A local name holding a colon reads as remote unless written as a path
# (one letter before the colon is a drive on a Windows host: the drive-relative test).
("ab:b.txt", "write it as ./name or as an absolute path"),
],
)
def test_a_path_that_names_no_registered_machine_is_refused_before_any_ssh(text, said):
with pytest.raises(RemotePathError, match=re.escape(said)):
parse(text, ROWS)
def test_two_rows_on_one_address_and_port_are_named_by_id_instead():
rows = [*ROWS, {**ROWS[0], "id": "box2-again"}]
with pytest.raises(RemotePathError, match=r"matches more than one registered machine .*write the machine's id"):
parse("scp://203.0.113.7:49273//x", rows)
assert parse("box2-again:/x", rows).machine == "box2-again"
@pytest.mark.parametrize(
"text",
[
"./a:b.txt",
"/abs/a:b",
"C:\\x",
"c:/x",
"C:",
"\\\\?\\C:\\x\\review_marker.txt",
"\\\\server\\share\\f",
"http://x.org/a",
"https://x.org/a:b",
"~/x",
"dir/a:b",
":x",
"",
],
)
def test_a_local_path_is_left_to_this_computer(text):
assert not looks_remote(text)
assert parse(text, ROWS) is None
def test_a_drive_relative_path_is_this_computer_s_on_a_windows_host_and_scp_s_elsewhere(monkeypatch):
monkeypatch.setattr(remote_path, "_WINDOWS", True)
assert not looks_remote("C:review_marker.txt") and parse("C:review_marker.txt", ROWS) is None
monkeypatch.setattr(remote_path, "_WINDOWS", False)
assert looks_remote("c:review_marker.txt"), "what scp reads as machine c"
# --- shown the way scp writes it ------------------------------------------------------
@pytest.mark.parametrize(
("text", "shown"),
[
# Not port 22 -- the URI form, absolute paths with two slashes.
("box2:/root/raven-work/t1/report.md", "scp://root@203.0.113.7:49273//root/raven-work/t1/report.md"),
("box2:raven-work/a", "scp://root@203.0.113.7:49273/raven-work/a"),
# Port 22 -- the short form.
("std:/data/x", "alice@198.51.100.9:/data/x"),
("std:notes", "alice@198.51.100.9:notes"),
("v6:/etc/hostname", "root@[2001:db8::5]:/etc/hostname"),
],
)
def test_a_path_is_shown_with_the_registry_s_user_address_and_port(text, shown):
assert parse(text, ROWS).display() == shown
@pytest.mark.parametrize(
"text", ["box2:/root/x", "box2:raven-work/a", "std:/data/x", "std:notes", "v6:/etc/hostname", "box2:"]
)
def test_what_is_shown_reads_back_as_the_same_file_on_the_same_machine(text):
first = parse(text, ROWS)
again = parse(first.display(), ROWS)
assert (again.machine, again.path) == (first.machine, first.path)
def test_a_path_is_shown_absolute_once_the_home_is_known():
where = parse("box2:raven-work/a", ROWS)
assert where.display(where.absolute("/root")) == "scp://root@203.0.113.7:49273//root/raven-work/a"
# --- placing a path on its machine ------------------------------------------------
@pytest.mark.parametrize(
("path", "placed"),
[
("", "/root"),
("~", "/root"),
("~/a", "/root/a"),
("a/b", "/root/a/b"),
("/x/../y", "/y"),
("raven-work/../.ssh/id", "/root/.ssh/id"),
("~bob/x", "~bob/x"),
],
)
def test_a_path_is_placed_against_the_login_home(path, placed):
assert absolute(path, "/root") == placed
def test_without_a_home_a_relative_path_stays_as_written():
assert absolute("a/b", "") == "a/b"
assert absolute("", "") == ""
@pytest.mark.parametrize(
("path", "root", "inside"),
[
("/root/raven-work", "/root/raven-work", True),
("/root/raven-work/t1/a", "/root/raven-work", True),
("/root/raven-work/t1/a", "/root/raven-work/", True),
("/root/raven-workx/a", "/root/raven-work", False),
("/root/raven-work/../.ssh/id", "/root/raven-work", False),
("raven-work/a", "/root/raven-work", False),
],
)
def test_within_compares_whole_directories(path, root, inside):
assert within(path, root) is inside
def test_only_an_absolute_home_is_remembered():
remote_path.remember_home("box2", "/root/")
remote_path.remember_home("box", "relative")
assert remote_path.known_home("box2") == "/root"
assert remote_path.known_home("box") == ""
# --- what a read reaches, for its permission check ----------------------------------
@pytest.fixture
def registry(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):
path = tmp_path / "connections.json"
path.write_text(json.dumps({"connections": ROWS}), encoding="utf-8")
monkeypatch.setenv("RAVEN_CONNECTIONS", str(path))
@pytest.fixture
def agents(monkeypatch: pytest.MonkeyPatch):
"""The acp entries config holds; none unless a test adds one."""
entries: list[SimpleNamespace] = []
monkeypatch.setattr(
"raven.config.loader.load_config", lambda *a, **k: SimpleNamespace(subagents=SimpleNamespace(agents=entries))
)
return entries
@pytest.mark.parametrize(
("tool", "path", "inside", "directory"),
[
("read_file", "box2:raven-work/t1/a.md", True, "~/raven-work/t1"),
("read_file", "box2:~/raven-work/t1/a.md", True, "~/raven-work/t1"),
("list_dir", "box2:raven-work", True, "~/raven-work"),
("grep", "box2:raven-work/t1", True, "~/raven-work/t1"),
("find", "box2:", False, "~"),
("read_file", "box2:.ssh/id_rsa", False, "~/.ssh"),
("read_file", "box2:raven-work/../.ssh/id_rsa", False, "~/.ssh"),
# Absolute with no home known yet: placed nowhere, so it asks.
("read_file", "box2:/root/raven-work/a", False, "/root/raven-work"),
# The row's own paths.
("read_file", "std:/data/shared/a/b.txt", True, "/data/shared/a"),
("list_dir", "std:/data", False, "/data"),
],
)
def test_a_read_reaches_one_directory_inside_the_roots_or_outside(registry, agents, tool, path, inside, directory):
reach = remote_path.reading(tool, {"path": path})
assert (reach.inside, reach.directory) == (inside, directory)
def test_once_the_home_is_known_an_absolute_path_is_placed_in_the_roots(registry, agents):
remote_path.remember_home("box2", "/root")
inside = remote_path.reading("read_file", {"path": "scp://root@203.0.113.7:49273//root/raven-work/t1/a"})
outside = remote_path.reading("read_file", {"path": "box2:/root/.ssh/id_rsa"})
assert (inside.inside, inside.directory) == (True, "/root/raven-work/t1")
assert (outside.inside, outside.directory) == (False, "/root/.ssh")
def test_an_agent_entry_s_remote_cwd_on_that_machine_is_a_root(registry, agents):
agents.append(SimpleNamespace(machine="box2", remote_cwd="/srv/agents"))
agents.append(SimpleNamespace(machine="box", remote_cwd="/elsewhere"))
agents.append(SimpleNamespace(machine=None, remote_cwd=None))
assert remote_path.reading("read_file", {"path": "box2:/srv/agents/t1/a"}).inside
assert not remote_path.reading("read_file", {"path": "box2:/elsewhere/a"}).inside
def test_a_config_that_cannot_load_adds_no_root(registry, monkeypatch):
def broken(*args, **kwargs):
raise ValueError("not json")
monkeypatch.setattr("raven.config.loader.load_config", broken)
assert remote_path.reading("read_file", {"path": "box2:raven-work/a"}).inside
assert not remote_path.reading("read_file", {"path": "box2:/srv/agents/a"}).inside
@pytest.mark.parametrize(
("tool", "params"),
[
("write_file", {"path": "box2:raven-work/a"}),
("read_file", {"path": "./notes.md"}),
("read_file", {"path": "nobox:/x"}),
("read_file", {"path": 7}),
("read_file", None),
("grep", {"pattern": "x"}),
],
)
def test_a_call_that_reaches_no_machine_has_no_reading(registry, agents, tool, params):
assert remote_path.reading(tool, params) is None
def test_the_fence_is_the_root_a_path_is_under_else_what_was_approved(registry, agents):
where = parse("box2:raven-work/t1/a", ROWS)
assert remote_path.fence("read_file", where, "/root/raven-work/t1/a", "/root") == "/root/raven-work"
assert remote_path.fence("read_file", where, "/root/.ssh/id_rsa", "/root") == "/root/.ssh"
assert remote_path.fence("list_dir", where, "/root/.ssh", "/root") == "/root/.ssh"
assert remote_path.fence("read_file", where, "/id", "/root") == "/"
def test_registered_paths_are_remote_without_a_debug_switch(registry, agents, monkeypatch):
monkeypatch.delenv("RAVEN_DEBUG_MACHINES", raising=False)
assert remote_path.target("box2:/root/x").machine == "box2"
assert remote_path.target("scp://root@203.0.113.7:49273//root/x").machine == "box2"
assert remote_path.reading("read_file", {"path": "box2:.ssh/id_rsa"}) is not None
def test_the_node_pool_is_whatever_assembly_handed_over():
assert remote_path.nodes() is None
pool = object()
remote_path.use_nodes(lambda: pool)
assert remote_path.nodes() is pool
# --- what a reader read ----------------------------------------------------------------
def test_what_was_read_is_the_reader_s_own_and_the_table_is_bounded(monkeypatch):
monkeypatch.setattr(remote_path, "_READ", {})
monkeypatch.setattr(remote_path, "_READ_LIMIT", 3)
remote_path.remember_read("box2", "/root/a", "d1", reader="A")
remote_path.remember_read("box2", "/root/a", "d2", reader="B")
assert remote_path.last_read("box2", "/root/a", reader="A") == "d1"
assert remote_path.last_read("box2", "/root/a", reader="B") == "d2"
assert remote_path.last_read("box2", "/root/a") is None, "no name is one more reader, not every reader"
remote_path.forget_read("box2", "/root/a", reader="A")
assert remote_path.last_read("box2", "/root/a", reader="A") is None
assert remote_path.last_read("box2", "/root/a", reader="B") == "d2", "A wrote: B's next overwrite is checked"
remote_path.remember_read("box2", "/root/b", "d3", reader="B")
remote_path.remember_read("box2", "/root/a", "d4", reader="B") # read again: the newest entry now
remote_path.remember_read("box2", "/root/c", "d5", reader="B")
remote_path.remember_read("box2", "/root/d", "d6", reader="B")
assert remote_path.last_read("box2", "/root/b", reader="B") is None, "past the bound the oldest goes"
assert [remote_path.last_read("box2", p, reader="B") for p in ("/root/a", "/root/c", "/root/d")] == [
"d4",
"d5",
"d6",
]