fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
"""The exec tool's background lane: harness custody for long local commands.
|
|
|
|
|
|
|
|
|
|
What separates this from ``nohup`` in the command string is everything the
|
|
|
|
|
tests below pin: the task has a handle, the log has a managed path the result
|
|
|
|
|
names, the child is tracked and reapable, and the lane changes none of the
|
|
|
|
|
rules -- the permission gate (which rules on the call before either lane is
|
|
|
|
|
chosen) and the sandbox boundary still have the last word.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
import asyncio
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
import json
|
|
|
|
|
import time
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from raven.agent.tools import background_exec
|
|
|
|
|
from raven.agent.tools.shell import ExecTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
|
|
|
|
def home(tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("RAVEN_HOME", str(tmp_path / "home"))
|
|
|
|
|
return tmp_path / "home"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _wait_for(predicate, timeout=5.0):
|
|
|
|
|
deadline = time.monotonic() + timeout
|
|
|
|
|
while time.monotonic() < deadline:
|
|
|
|
|
if predicate():
|
|
|
|
|
return True
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---- the module: custody itself ----
|
|
|
|
|
|
|
|
|
|
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
def _task_id_from(out: str) -> str:
|
|
|
|
|
"""The handle out of whichever note the launch produced.
|
|
|
|
|
|
|
|
|
|
Both notes name it the same way, but the ended one puts the command after
|
|
|
|
|
it, so slicing on a trailing space would take part of the command too.
|
|
|
|
|
"""
|
|
|
|
|
return out.split(" as ", 1)[1].split(" ", 1)[0].rstrip(",.")
|
|
|
|
|
|
|
|
|
|
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
def test_a_started_task_runs_detached_and_its_log_is_managed(home):
|
|
|
|
|
task = background_exec.start("echo hello-from-behind")
|
|
|
|
|
|
|
|
|
|
assert task.task_id.startswith("bg-")
|
|
|
|
|
assert Path(task.log_path).parent == home / "background"
|
|
|
|
|
assert _wait_for(lambda: "hello-from-behind" in background_exec.tail(task.task_id))
|
|
|
|
|
record = json.loads((home / "background" / f"{task.task_id}.json").read_text())
|
|
|
|
|
assert record["pid"] == task.pid and record["command"] == "echo hello-from-behind"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_status_reports_liveness_and_reap_ends_the_group(home):
|
|
|
|
|
task = background_exec.start("sleep 30")
|
|
|
|
|
|
|
|
|
|
live = background_exec.status(task.task_id)
|
|
|
|
|
assert live is not None and live["running"] is True
|
|
|
|
|
assert any(t["task_id"] == task.task_id for t in background_exec.running())
|
|
|
|
|
|
|
|
|
|
assert background_exec.reap(task.task_id) is True
|
|
|
|
|
assert _wait_for(lambda: not background_exec.status(task.task_id)["running"])
|
|
|
|
|
assert background_exec.reap(task.task_id) is False, "a dead task is not signalled again"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_an_unknown_task_is_none_not_a_traceback(home):
|
|
|
|
|
assert background_exec.status("bg-nope") is None
|
|
|
|
|
assert background_exec.tail("bg-nope") == ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_the_start_note_hands_over_the_handle_and_the_log(home):
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
task = background_exec.start("sleep 30")
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert task.task_id in note
|
|
|
|
|
assert task.log_path in note
|
|
|
|
|
assert "later one" in note, "the model is told how completion becomes visible"
|
|
|
|
|
assert "reminder" in note, "the road back without holding the turn is named"
|
|
|
|
|
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
background_exec.reap(task.task_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_command_that_exits_at_once_is_reported_as_ended_not_started(home):
|
|
|
|
|
"""The bug this file grew the check for: `start` returns when `Popen` does,
|
|
|
|
|
so a command that never ran -- a port taken, a binary missing -- was handed
|
|
|
|
|
back as running, and the model went looking for its own bug in a server that
|
|
|
|
|
had never bound (measured 2026-09-22, session 692e5c)."""
|
|
|
|
|
task = background_exec.start("echo one-line-then-out; exit 3")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "ended immediately" in note
|
|
|
|
|
assert "exited with code 3" in note
|
|
|
|
|
assert "one-line-then-out" in note, "the log's tail is the diagnosis and comes with it"
|
|
|
|
|
assert "not running now" in note
|
|
|
|
|
assert "This turn is not held open" not in note, "nothing is running to read a log of"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_command_killed_by_a_signal_is_named_by_that_signal(home):
|
|
|
|
|
task = background_exec.start("kill -TERM $$")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "killed by SIGTERM" in note
|
|
|
|
|
assert "ended immediately" in note
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_command_that_wrote_nothing_says_so_rather_than_showing_an_empty_log(home):
|
|
|
|
|
task = background_exec.start("exit 1")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "It wrote nothing to its log." in note
|
|
|
|
|
assert "exited with code 1" in note
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_binary_that_is_not_there_ends_the_launch_rather_than_hanging_around(home):
|
|
|
|
|
task = background_exec.start("raven-test-no-such-binary --serve")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "ended immediately" in note
|
|
|
|
|
assert "not found" in note, "the shell's own words are the whole explanation"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_command_that_keeps_running_is_not_claimed_to_have_ended(home):
|
|
|
|
|
"""The check may not become a guess in the other direction: a task still up
|
|
|
|
|
after the window is reported as running, which is all the window shows."""
|
|
|
|
|
task = background_exec.start("sleep 30")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "Started in the background" in note
|
|
|
|
|
assert "ended immediately" not in note
|
|
|
|
|
assert background_exec.status(task.task_id)["running"] is True
|
|
|
|
|
|
|
|
|
|
background_exec.reap(task.task_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_an_ended_launch_leaves_nothing_to_reap(home):
|
|
|
|
|
task = background_exec.start("exit 7")
|
|
|
|
|
note = background_exec.start_note(task)
|
|
|
|
|
|
|
|
|
|
assert "nothing will be reaped later" in note
|
|
|
|
|
assert background_exec.status(task.task_id)["running"] is False
|
|
|
|
|
assert background_exec.reap(task.task_id) is False
|
|
|
|
|
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
|
|
|
|
|
# ---- the tool wiring: the lane changes custody, never the rules ----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_exec_background_returns_the_note_without_holding_the_turn(home, tmp_path):
|
|
|
|
|
tool = ExecTool(working_dir=str(tmp_path))
|
|
|
|
|
t0 = time.monotonic()
|
|
|
|
|
|
|
|
|
|
out = await tool.execute(command="sleep 10", run_in_background=True)
|
|
|
|
|
|
|
|
|
|
assert time.monotonic() - t0 < 5, "the turn is not held for the command"
|
|
|
|
|
assert "Started in the background as bg-" in out
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
task_id = _task_id_from(out)
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
assert background_exec.reap(task_id) is True
|
|
|
|
|
|
|
|
|
|
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
@pytest.mark.parametrize(("command", "failed"), [("exit 7", True), ("exit 0", False), ("sleep 30", False)])
|
|
|
|
|
async def test_the_registry_reads_an_early_exit_as_the_call_s_verdict(home, tmp_path, command, failed):
|
|
|
|
|
"""An exit the launch saw is the call's result, as it is on the synchronous
|
|
|
|
|
lane: nonzero is a failed call, zero and still-running are not."""
|
|
|
|
|
from raven.agent.tools.registry import ToolRegistry, call_failed
|
|
|
|
|
|
|
|
|
|
registry = ToolRegistry()
|
|
|
|
|
registry.register(ExecTool(working_dir=str(tmp_path)))
|
|
|
|
|
|
|
|
|
|
out = await registry.execute("exec", {"command": command, "run_in_background": True})
|
|
|
|
|
|
|
|
|
|
assert call_failed(out) is failed
|
|
|
|
|
if command.startswith("sleep"):
|
|
|
|
|
assert background_exec.reap(_task_id_from(out)) is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_the_launch_check_waits_off_the_event_loop(home, tmp_path):
|
|
|
|
|
"""The note waits up to a second for the command to contradict it. A launch
|
|
|
|
|
that keeps running spends that whole second, and on the loop's own thread
|
|
|
|
|
every other turn in the process would stall for it."""
|
|
|
|
|
tool = ExecTool(working_dir=str(tmp_path))
|
|
|
|
|
beats = [time.monotonic()]
|
|
|
|
|
|
|
|
|
|
async def ticker():
|
|
|
|
|
while True:
|
|
|
|
|
await asyncio.sleep(0.05)
|
|
|
|
|
beats.append(time.monotonic())
|
|
|
|
|
|
|
|
|
|
beat = asyncio.create_task(ticker())
|
|
|
|
|
try:
|
|
|
|
|
out = await tool.execute(command="sleep 30", run_in_background=True)
|
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
finally:
|
|
|
|
|
beat.cancel()
|
|
|
|
|
|
|
|
|
|
gaps = [later - earlier for earlier, later in zip(beats, beats[1:])]
|
|
|
|
|
assert "Started in the background as bg-" in out
|
|
|
|
|
assert max(gaps) < 0.5, f"the loop stalled for {max(gaps):.2f}s while the launch was confirmed"
|
|
|
|
|
assert background_exec.reap(_task_id_from(out)) is True
|
|
|
|
|
|
|
|
|
|
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_machine_and_background_never_combine(home, tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setattr("raven.ops.connections.load", lambda: [{"id": "conn_gpu"}])
|
|
|
|
|
tool = ExecTool(working_dir=str(tmp_path))
|
|
|
|
|
|
|
|
|
|
out = await tool.execute(command="rsync a b:", machine="conn_gpu", run_in_background=True)
|
|
|
|
|
|
|
|
|
|
assert "do not combine" in out
|
|
|
|
|
assert "ops_submit" in out, "background work on a registered machine is a job"
|
|
|
|
|
assert "Nothing was run" in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_a_sandboxed_session_refuses_the_lane(home, tmp_path):
|
|
|
|
|
class _Sandboxed:
|
|
|
|
|
is_sandboxed = True
|
|
|
|
|
|
|
|
|
|
async def exec(self, *a, **kw):
|
|
|
|
|
raise AssertionError("the refusal must come before any execution")
|
|
|
|
|
|
|
|
|
|
tool = ExecTool(working_dir=str(tmp_path), executor=_Sandboxed())
|
|
|
|
|
|
|
|
|
|
out = await tool.execute(command="echo hi", run_in_background=True)
|
|
|
|
|
|
|
|
|
|
assert "not available in a sandboxed session" in out
|
|
|
|
|
assert "outside the sandbox" in out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_the_deny_list_still_has_the_last_word(home, tmp_path):
|
|
|
|
|
"""The refusal comes from the permission gate at the registry door, which is
|
|
|
|
|
upstream of both lanes, so the background one is not a way around it."""
|
|
|
|
|
from raven.agent.tools.registry import ToolRegistry
|
|
|
|
|
from raven.config.schema import PermissionsConfig
|
|
|
|
|
from raven.permissions import BuiltinRulings, PermissionGate
|
|
|
|
|
|
|
|
|
|
registry = ToolRegistry(
|
|
|
|
|
permission_gate=PermissionGate(
|
|
|
|
|
config_source=lambda: PermissionsConfig(mode="full"),
|
|
|
|
|
builtin=BuiltinRulings(),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
registry.register(ExecTool(working_dir=str(tmp_path)))
|
|
|
|
|
|
|
|
|
|
out = await registry.execute("exec", {"command": "mkfs /dev/sda", "run_in_background": True})
|
|
|
|
|
|
|
|
|
|
assert "safety guard" in str(out)
|
|
|
|
|
assert not background_exec.running(), "a blocked command must not start in the background either"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
|
async def test_the_background_child_gets_the_baseline_env_not_the_host_env(home, tmp_path, monkeypatch):
|
|
|
|
|
monkeypatch.setenv("SECRET_TOKEN", "must-not-leak")
|
|
|
|
|
tool = ExecTool(working_dir=str(tmp_path))
|
|
|
|
|
|
|
|
|
|
out = await tool.execute(command="env", run_in_background=True)
|
|
|
|
|
|
fix(agent): a background command that dies at once is reported as ended, not started (#720)
## Summary
A background `exec` launch (`run_in_background=True`) returned as soon
as `Popen` did and always told the model the command was running. A
command that fails at once - a port already bound, a missing binary -
was handed back as a live task. In session 692e5c (2026-09-22) `python
-m http.server 8765` lost the port and exited, and the agent went on
debugging its client against a server that never bound.
- `start_note` now waits up to 0.9s (`_CONFIRM_S`) for the process to
contradict the launch. A command that ended in that window gets an
"ended immediately" note with its exit code (or the signal that killed
it) and the last 2000 bytes of its log, and is named as not running and
not reaped later. A command still alive after the window gets the
existing running note, which claims no more than the window shows.
- The wait runs in a worker thread (`asyncio.to_thread` in
`ExecTool.execute`), so a launch that keeps running does not stall the
event loop, and every other turn in the gateway with it, for that
second.
- The note carries the exit verdict: `start_note` returns a `ToolOutput`
with `ok=False` for a nonzero exit seen in the window, so the registry,
the tool event and every success-sensitive consumer record the call as
failed, as the synchronous lane does. An exit 0 inside the window and a
task still running stay `ok=True`.
- The env-hygiene test now reads the child's output from the launch
note, since an `env` that finishes inside the window is reported as
ended with its log attached.
## Type
- [x] Fix
- [ ] Feature
- [ ] Docs
- [ ] CI / tooling
- [ ] Refactor
- [ ] Other
## Verification
Head e6183de67, rebased on origin/main 8ee5ae0d8; `git range-diff` shows
both commits replayed unchanged, and `tests/test_shell_background.py`
reran at this head: 23 passed. The other numbers below are from
ea9d6c209 on afdc04f89.
```
uv run --frozen --python 3.12 --all-extras pytest -q tests/test_shell_background.py tests/test_tool_registry*.py tests/test_shell*.py
615 passed
COLORTERM=truecolor uv run --frozen --python 3.12 --all-extras pytest -q -p no:cacheprovider
25910 passed, 109 skipped, 1 failed: tests/test_rpc_files.py::test_a_host_without_libreoffice_says_so
(local only and pre-existing: it fails the same way on origin/main afdc04f89 on this machine, which has
LibreOffice installed; this branch does not touch rpc/files)
uv run --frozen --python 3.12 --extra dev ruff check raven tests -> All checks passed
uv run --frozen --python 3.12 --extra dev ruff format --check raven/agent/tools tests/test_shell_background.py -> already formatted
make lint-types -> exit 0
git diff --check origin/main...HEAD -> clean
PYTHONPATH=. uv run --frozen --python 3.12 --extra dev python scripts/check_source_language.py origin/main...HEAD -> exit 0
make check-commits; make check-pr-title; COMMIT_RANGE=origin/main...HEAD make check-large-files -> exit 0
git merge-tree --write-tree HEAD origin/main -> clean
```
Revert-to-red, run with `tests/test_shell_background.py`:
- `start_note` reverted to the unconditional running note: 7 failed (the
six ended-note tests plus the env-hygiene test).
- `ExecTool` calling `start_note` on the loop thread instead of
`asyncio.to_thread`: 1 failed,
`test_the_launch_check_waits_off_the_event_loop` (it measures the
largest gap between 50 ms ticks of a concurrent task, about 0.9s when
blocked).
- `start_note` returning the ended note without `ok=`: 1 failed,
`test_the_registry_reads_an_early_exit_as_the_call_s_verdict[exit
7-True]` (it goes through `ToolRegistry.execute` and `call_failed`).
Not run: `make coverage-diff`, and the integration suite (`-m
integration`); this change does not touch an integration path.
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots are updated when needed (no doc
describes the launch note)
## Risk
- Every successful background launch now returns about 0.9s later than
before; the turn still does not wait for the command itself.
- A command that legitimately finishes within 0.9s (for example `echo`)
is now reported as ended, with its output, instead of as a background
task to poll. That is the accurate answer, and nothing is left to reap.
- Rollback: revert the commit; no state or config format changes.
- [x] Security impact considered
- [x] Backward compatibility considered
- [x] Rollback path is clear for risky changes
## Related Issues
N/A
---------
Co-authored-by: Claude (claude-opus-5-5) <noreply@anthropic.com>
2026-09-23 22:57:35 +08:00
|
|
|
assert "PATH=" in out, "the child got an allowlisted environment and its output came back"
|
|
|
|
|
assert "must-not-leak" not in out, "the detached child keeps the executor's allowlist hygiene"
|
|
|
|
|
assert not background_exec.running(), "the command is over; nothing is left to reap"
|
fix(tools): an array sent as its own JSON is still an array
## Summary
Reported as "deliver_files is broken". It is not: **the tool never ran.**
Every `deliver_files` call on one install was refused before dispatch -- five in a row in a single session -- and nothing was ever delivered:
```
Error: Invalid parameters for tool 'deliver_files': files should be array
```
What the model sent was the array, spelled as a string:
```json
{"files": "[{\"path\": \"...poster.png\", \"title\": \"...\"}]", "message": "..."}
```
`cast_params` already forgives that spelling for every scalar type -- `"3"` for an integer, `"true"` for a boolean -- and did not for arrays or objects, which are exactly the types a model is most likely to stringify.
## Type
- [x] Fix
- [ ] Feature
## The edge of the forgiving is the design
When the value is a string, try `json.loads`, and take the result **only if it is the type the schema asked for**. A string that is not JSON, or is JSON of the wrong shape, is returned unchanged so the validator still names the real problem -- the point is to widen what is accepted, not to swallow a type error.
**Nothing here can raise.** Turning "your argument has the wrong type" into a traceback would be worse than the refusal it replaces, so the parse is wrapped and failure falls through.
**A `type: "string"` field is never parsed.** JSON is ordinary text in a message body or a file's content, and replacing it would hand the tool something it never asked for. Two independent things prevent it today (the string branch returns before the parse is reached, and the parse only wins on a type match), which is why the case pinning it is a tripwire rather than a load-bearing assertion -- said so in its own docstring rather than left to look like coverage.
## Not just deliver_files
Six built-in tools declare an array or object parameter and were all exposed:
| tool | parameter |
|---|---|
| `deliver_files` | `files` |
| `ask_user` | `questions` |
| `image_generate` | `images` |
| `message` | `media` |
| `create_playbook` | `skills` |
| `video_generate` | `params` |
The same failure was already seen from the other side: `acp_client/acp_agent.py` records `update_task_state` and `operations should be array` from a real dispatch.
## Verification
```
make lint-python ruff check + ruff format --check, 1850 files
make lint-imports 9 contracts kept, 0 broken
pytest tests/ 19,699 passed, 7 failed (all pre-existing, see below)
```
- [x] Relevant tests pass locally
- [x] Relevant lint / type checks pass locally
- [ ] User-facing docs or screenshots -- no user-facing string
**End to end, through the real tool**, with the exact payload that failed five times in production:
```
files sent as: str -> [{"path": ".../poster.png", "title": "..."}]
returned: Delivered 1 file(s): poster.png (72 B).
still "should be array": False
registered in the store: True
```
Twelve cases, five mutations, each killing at least one:
| mutation | red |
|---|---|
| drop the whole branch (the old behaviour) | 3 |
| raise instead of falling through on a parse failure | 5 |
| accept the parse whatever type it yielded | 1 |
| do not re-cast the parsed value, so items are not cast | 1 |
| widen the tuple to include `string` | **survived -- unreachable** |
The last is worth stating rather than hiding: that mutation cannot change behaviour, because the `type == "string"` branch returns earlier and the type check would refuse the result anyway. I also tried moving the new block above that branch, alone and combined with widening the tuple, and neither reddens anything either. The invariant holds by construction; the case that documents it says so.
**Pre-existing failures, checked against the base.** 7 failed in the full run, all in `tests/test_security_network.py` (`test_a_redirect_to_a_dangerous_address_is_refused` and siblings). Running that file alone on this same tree gives 4 -- the count is order-dependent -- and the change touches only `raven/agent/tools/params.py` plus a new test file, neither of which `raven/security/network.py` imports. The failures are the box's resolver: it returns poisoned answers roughly half the time, which I measured separately (`en.wikipedia.org` came back as `2001::1` on 3 of 6 lookups). Not introduced here, and worth someone's attention on its own.
## Risk
- [x] Security impact considered -- widens what reaches a tool's own validation, not what bypasses it. A parsed value is still validated against the same schema, and a parse that yields the wrong type is refused exactly as before
- [x] Backward compatibility considered -- a call that worked before takes the identical path; the new branch is reached only by a `str` where an array or object is declared, which previously always failed
- [x] Rollback path is clear -- single-commit revert
## Related Issues
N/A
## Swept
`mr-review-patterns` pre-submit sweep, over `git diff origin/main...HEAD`.
- **1 (a write location moved)** -- nothing moved; a branch was added to an existing dispatch on `target_type`. Grepped every caller of `cast_params`: the registry is the only one, and it runs `validate_params` right after, which is the reader this affects.
- **2 (a value with several entry points)** -- the cast is the single funnel every tool argument passes through, which is why the fix belongs here and not in `deliver_files`.
- **3 (a guard)** -- checked both directions: what it wrongly lets through (JSON of the wrong type, non-JSON text -- both refused, with cases) and what it wrongly refuses (a real list, unaffected, with a case).
- **4 (tests)** -- five mutations above, one honestly reported as unreachable. Failing-ID sets diffed against the base rather than counts.
- **6 (stale text)** -- the module docstring enumerated what is coerced ("a string that should be an integer"); it now names arrays and objects too. Grepped the tracked tree for other statements of the rule: none.
- **9 (error handling)** -- the new path adds one way to end and it is caught: `json.loads` raises `ValueError` on bad text and `TypeError` on a non-string, and both fall through to the untouched value.
- **11** -- `git ls-files | grep -c node_modules` is 0; the commit-message gate was run, not eyeballed.
Not applicable, with reasons: **3b, 3c, 3d, 3e** (no tool schema produced, no state moved, one lane, no assertion lost its symbol); **4b, 4c, 4d** (no platform branch, no shell, no rendered number); **5, 5b** (no dependency; `json` is stdlib and already imported across this package); **6b, 6c** (no backup, no generated artifact); **7, 7b** (nothing on disk); **8** (no asset, no secret); **10** (no prompt text).
## Deliberately not fixed
The error text still ends with `[Analyze the error above and try a different approach.]`, and the model plainly did not -- it sent the same shape five times. A message naming the expected spelling (`files must be a JSON array, not a string containing one`) would give it something to act on. That is a change to the registry's error surface, affects every tool, and deserves its own argument.
## Moved from !594
Opened on a compliant source branch. AGENTS.md 2.1 asks for a 3-5 word snake_case description and the previous branch had seven; GitLab cannot change an existing merge request's source branch, so the change moves rather than the name.
Same commits, rebased onto `c2a9768f`. Both code findings from the review on !594 are already in them -- `RecursionError` caught with its regression case, and the implementation comment reduced to the two non-obvious reasons. Nothing else changed, so that review carries.
2026-09-09 15:42:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_session_exit_reaps_only_its_own_tasks(home, monkeypatch):
|
|
|
|
|
task = background_exec.start("sleep 30")
|
|
|
|
|
monkeypatch.setattr(background_exec, "_OWN", [])
|
|
|
|
|
|
|
|
|
|
background_exec._reap_own()
|
|
|
|
|
|
|
|
|
|
assert background_exec.status(task.task_id)["running"] is True, (
|
|
|
|
|
"a survivor from another session is the operator's to keep or broom (reap_all)"
|
|
|
|
|
)
|
|
|
|
|
background_exec.reap(task.task_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_recorded_pid_this_process_did_not_start_is_believed_only_while_it_leads_its_own_group(home):
|
|
|
|
|
"""Reviewer 2026-09-07: a record naming a pid this process never started
|
|
|
|
|
was reported running, listed, and reaped -- and nothing ever retired it, so
|
|
|
|
|
once the kernel reused the number that was an unrelated process group.
|
|
|
|
|
A task launched by ``start`` leads its own session; a recycled pid does
|
|
|
|
|
not, so the probe asks for that, and a record found dead is retired."""
|
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
bystander = subprocess.Popen(["sleep", "30"]) # in OUR group: what a recycled pid looks like
|
|
|
|
|
try:
|
|
|
|
|
record = {
|
|
|
|
|
"task_id": "bg-stale-0",
|
|
|
|
|
"command": "sleep 30",
|
|
|
|
|
"pid": bystander.pid,
|
|
|
|
|
"log_path": str(home / "background" / "bg-stale-0.log"),
|
|
|
|
|
"started_at": 0.0,
|
|
|
|
|
}
|
|
|
|
|
(home / "background").mkdir(parents=True, exist_ok=True)
|
|
|
|
|
(home / "background" / "bg-stale-0.json").write_text(json.dumps(record))
|
|
|
|
|
|
|
|
|
|
assert background_exec.status("bg-stale-0")["running"] is False
|
|
|
|
|
assert background_exec.reap("bg-stale-0") is False
|
|
|
|
|
assert bystander.poll() is None, "a bare pid from a stale record is not signalled"
|
|
|
|
|
assert all(t["task_id"] != "bg-stale-0" for t in background_exec.running())
|
|
|
|
|
assert not (home / "background" / "bg-stale-0.json").exists(), "a record found dead is retired"
|
|
|
|
|
finally:
|
|
|
|
|
bystander.kill()
|
|
|
|
|
bystander.wait()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_a_task_from_another_session_is_still_seen_and_reaped_through_its_group(home, monkeypatch):
|
|
|
|
|
"""The other half: a real survivor -- leading its own session, as start()
|
|
|
|
|
launches it -- is still reported, listed and stopped when this process
|
|
|
|
|
holds no handle for it."""
|
|
|
|
|
task = background_exec.start("sleep 30")
|
|
|
|
|
handle = background_exec._PROCS[task.task_id] # kept so the reaped child can be waited on below
|
|
|
|
|
monkeypatch.setattr(background_exec, "_PROCS", {})
|
|
|
|
|
monkeypatch.setattr(background_exec, "_OWN", [])
|
|
|
|
|
|
|
|
|
|
assert background_exec.status(task.task_id)["running"] is True
|
|
|
|
|
assert any(t["task_id"] == task.task_id for t in background_exec.running())
|
|
|
|
|
assert background_exec.reap(task.task_id) is True
|
|
|
|
|
# This process is still its parent, so until it is waited on the child is
|
|
|
|
|
# a zombie, and a zombie still answers getpgid on Linux. Another session's
|
|
|
|
|
# real survivor has another parent (or init) to do this.
|
|
|
|
|
assert handle.wait(timeout=5) < 0 or handle.returncode != 0
|
|
|
|
|
assert not background_exec._leads_its_own_group(task.pid)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_the_schema_advertises_the_lane(home, tmp_path):
|
|
|
|
|
prop = ExecTool(working_dir=str(tmp_path)).parameters["properties"]["run_in_background"]
|
|
|
|
|
assert prop["type"] == "boolean"
|
|
|
|
|
assert "ops_submit" in prop["description"], "the boundary is taught where the parameter is read"
|
|
|
|
|
assert "timeout" in prop["description"], "that the lane reads no timeout is said where the model can see it"
|