Raven-1/tests/test_agents_code_flow_manifest.py
Handsome-wzw 7b45527b5b 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

345 lines
14 KiB
Python

"""The workspace report raven-code files for its host: the harness manifest.
Every field is machine-read from the working directory -- git facts measured
against the base the session ledger pinned at the session's first turn --
and nothing comes from model prose. The report rides the turn's observer
stash under ``acp_meta`` (the loop_hooks paper) and reaches the ACP client
as the prompt response's ``_meta["raven.harnessManifest"]``.
"""
import subprocess
import sys
from pathlib import Path
import pytest
from raven.agent import workdir
from raven.agent.loop import AgentLoop
from raven.agent.loop.bundles import EngineWiring, HostWiring, ToolWiring, TurnPolicy
from raven.config.raven import CheckpointConfig, RuntimeConfig
from raven.contracts.loop_hooks import AgentHookContext
from raven.providers.base import LLMProvider, LLMResponse
from raven.spine.message import ChatType, Source
from raven.spine.turn import Origin, TurnRequest
REPO = Path(__file__).resolve().parent.parent
PLUGIN_DIR = REPO / "agents" / "raven-code" / "plugins" / "code-flow"
sys.path.insert(0, str(PLUGIN_DIR))
from code_flow.flow import MANIFEST_META_KEY, CodeFlowHook # noqa: E402
from code_flow.manifest import MANIFEST_FIELDS, build_manifest # noqa: E402
from code_flow.sessions import SessionLedger # noqa: E402
def git(path: Path, *args: str) -> str:
return subprocess.run(["git", "-C", str(path), *args], check=True, capture_output=True, text=True).stdout.strip()
def make_repo(path: Path) -> str:
path.mkdir(parents=True, exist_ok=True)
subprocess.run(["git", "init", "-q", "-b", "main", str(path)], check=True)
git(path, "config", "user.email", "t@t")
git(path, "config", "user.name", "t")
(path / "a.py").write_text("x = 1\n")
git(path, "add", "-A")
git(path, "commit", "-qm", "init")
return git(path, "rev-parse", "HEAD")
def test_the_report_has_exactly_the_v1_fields(tmp_path):
base = make_repo(tmp_path / "repo")
report = build_manifest("acp:s1", tmp_path / "repo", base)
assert set(report) == set(MANIFEST_FIELDS)
assert report["manifestVersion"] == 1
assert report["sessionId"] == "acp:s1"
assert report["scope"] == "workspace"
assert (report["attribution"], report["sharedWith"]) == ("session", 0)
def test_a_clean_checkout_with_nothing_past_base_reports_no_changes(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
report = build_manifest("s", repo, base)
assert report["workspaceKind"] == "repository"
assert report["repositoryRoot"] == str(repo.resolve())
assert report["workdir"] == str(repo)
assert report["branch"] == "main"
assert report["baseCommit"] == base and report["head"] == base
assert report["commitsPastBase"] == {"count": 0, "items": []}
assert report["workingTree"] == {"clean": True, "uncommittedCount": 0, "entries": []}
assert report["status"] == "no_changes"
assert report["readyForIntegration"] is False
assert report["blockers"] == ["no commits past base"]
def test_uncommitted_work_reports_needs_commit(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
(repo / "a.py").write_text("x = 2\n")
(repo / "new.txt").write_text("n\n")
report = build_manifest("s", repo, base)
assert report["workingTree"]["clean"] is False
assert report["workingTree"]["uncommittedCount"] == 2
assert sorted(report["workingTree"]["entries"]) == [" M a.py", "?? new.txt"]
assert report["status"] == "needs_commit"
assert report["readyForIntegration"] is False
assert report["blockers"] == ["uncommitted changes must be committed"]
def test_committed_work_past_base_reports_ready_for_integration(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
(repo / "b.py").write_text("y = 2\n")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "add b")
head = git(repo, "rev-parse", "HEAD")
report = build_manifest("s", repo, base)
assert report["head"] == head and report["baseCommit"] == base
assert report["commitsPastBase"]["count"] == 1
assert report["commitsPastBase"]["items"] == [{"sha": head, "subject": "add b"}]
assert "1 file changed" in report["diffStat"]
assert report["status"] == "ready_for_integration"
assert report["readyForIntegration"] is True
assert report["blockers"] == []
@pytest.fixture
def outside_any_repo(tmp_path, monkeypatch):
"""A plain directory is only plain if git stops looking upward at it: the
test tree may itself sit inside some checkout."""
monkeypatch.setenv("GIT_CEILING_DIRECTORIES", str(tmp_path))
return tmp_path
def test_a_plain_directory_is_reported_as_such(outside_any_repo):
plain = outside_any_repo / "plain"
plain.mkdir()
report = build_manifest("s", plain, None)
assert report["workspaceKind"] == "plain"
assert report["repositoryRoot"] is None
assert report["workdir"] == str(plain)
assert report["baseCommit"] is None and report["head"] is None and report["branch"] is None
assert report["status"] == "unknown"
assert report["readyForIntegration"] is False
assert report["blockers"] == ["not a git repository"]
def test_an_unreadable_directory_degrades_to_unknown_never_to_ready(tmp_path):
report = build_manifest("s", tmp_path / "does-not-exist", "abc")
assert report["status"] == "unknown"
assert report["readyForIntegration"] is False
assert report["blockers"], "a failure names itself"
def test_a_repository_without_a_pinned_base_cannot_be_ready(tmp_path):
"""No base means no measure of what this session added: known facts are
reported, readiness is not claimed."""
repo = tmp_path / "repo"
head = make_repo(repo)
report = build_manifest("s", repo, None)
assert report["head"] == head and report["baseCommit"] is None
assert report["commitsPastBase"] == {"count": 0, "items": []}
assert report["status"] == "unknown"
assert report["readyForIntegration"] is False
assert "base commit" in report["blockers"][0]
def test_long_listings_are_capped_with_the_count_kept_whole(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
for n in range(105):
(repo / f"f{n:03}.txt").write_text("x\n")
report = build_manifest("s", repo, base)
assert report["workingTree"]["uncommittedCount"] == 105
assert len(report["workingTree"]["entries"]) == 100
@pytest.mark.asyncio
async def test_the_send_files_the_report_under_the_acp_meta_stash(tmp_path):
"""Measured against the base the ledger pinned at the first turn: a
commit made during the turn shows as work past base."""
repo = tmp_path / "repo"
base = make_repo(repo)
ledger = SessionLedger()
hook = CodeFlowHook(ledger=ledger)
with workdir.bind(repo):
await hook.before_user_inbound(AgentHookContext(session_key="acp:s1", inbound_content="add b"))
(repo / "b.py").write_text("y = 2\n")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "add b")
metadata: dict = {}
await hook.after_send(AgentHookContext(session_key="acp:s1", outbound_content="done", metadata=metadata))
report = metadata["observers"]["acp_meta"][MANIFEST_META_KEY]
assert MANIFEST_META_KEY == "raven.harnessManifest"
assert report["sessionId"] == "acp:s1"
assert report["workdir"] == str(repo)
assert report["baseCommit"] == base
assert report["commitsPastBase"]["count"] == 1
assert report["status"] == "ready_for_integration"
@pytest.mark.asyncio
async def test_the_send_leaves_other_observers_stashes_alone(outside_any_repo):
plain = outside_any_repo / "plain"
plain.mkdir()
hook = CodeFlowHook(ledger=SessionLedger())
metadata = {"observers": {"oncall_flow": {"tokens": 3}, "acp_meta": {"vendor.other": {"k": 1}}}}
with workdir.bind(plain):
await hook.before_user_inbound(AgentHookContext(session_key="s", inbound_content="t"))
await hook.after_send(AgentHookContext(session_key="s", outbound_content="d", metadata=metadata))
assert metadata["observers"]["oncall_flow"] == {"tokens": 3}
assert metadata["observers"]["acp_meta"]["vendor.other"] == {"k": 1}
assert metadata["observers"]["acp_meta"][MANIFEST_META_KEY]["workspaceKind"] == "plain"
class _AnswerProvider(LLMProvider):
"""One plain answer, no tools."""
def __init__(self) -> None:
super().__init__(api_key="test")
async def chat(
self,
messages,
tools=None,
model=None,
max_tokens=4096,
temperature=0.7,
reasoning_effort=None,
tool_choice=None,
):
return LLMResponse(content="added b.py", finish_reason="stop")
def get_default_model(self) -> str:
return "stub"
@pytest.mark.asyncio
async def test_a_turn_through_the_trunk_loop_files_the_report_on_its_record(tmp_path):
"""End to end on the trunk loop: the hook rides the host wiring, the
inbound phase books the session, the send files the report, and the
loop stamps it onto the turn's last assistant message in the session
record -- the very message the ACP layer reads back for ``_meta``."""
repo = tmp_path / "repo"
base = make_repo(repo)
agent = AgentLoop(
provider=_AnswerProvider(),
workspace=tmp_path / "home",
model="stub",
policy=TurnPolicy(max_iterations=4),
tools=ToolWiring(restrict_to_workspace=True),
engine=EngineWiring(runtime_config=RuntimeConfig(checkpoint=CheckpointConfig(policy="never"))),
host=HostWiring(hooks=[CodeFlowHook(ledger=SessionLedger())]),
)
with workdir.bind(repo):
out = await agent._process_message(
TurnRequest(
origin=Origin.USER,
source=Source(channel="acp", chat_id="s1", sender_id="user", chat_type=ChatType.DM),
text="add b.py",
),
session_key="acp:s1",
)
assert out is not None and out[0] == "added b.py"
record = agent.sessions.get_or_create("acp:s1")
stamped = [m for m in record.messages if m.get("role") == "assistant" and "observers" in m]
assert len(stamped) == 1
report = stamped[-1]["observers"]["acp_meta"][MANIFEST_META_KEY]
assert report["sessionId"] == "acp:s1"
assert report["workdir"] == str(repo)
assert report["baseCommit"] == base
assert report["status"] == "no_changes", report["workingTree"]
# --------------------------------------------------------------------------- #
# attribution: HEAD is shared, so commits past base are one session's only #
# while no other session shared the directory #
# --------------------------------------------------------------------------- #
def test_commits_past_base_are_not_attributed_when_the_directory_was_shared(tmp_path):
"""Two sessions on one checkout, no worktree between them: A commits, B did
nothing. ``base..HEAD`` holds A's commit for both. B's report must not
claim it, and must not come out ready."""
repo = tmp_path / "repo"
base = make_repo(repo)
(repo / "b.py").write_text("y = 2\n")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "A's commit")
report = build_manifest("acp:B", repo, base, shared_with=1)
assert report["attribution"] == "shared"
assert report["sharedWith"] == 1
assert report["commitsPastBase"]["count"] == 1, "the workspace fact is still reported"
assert report["status"] == "shared_workspace"
assert report["readyForIntegration"] is False
assert "cannot be attributed" in report["blockers"][0]
def test_a_shared_report_still_names_uncommitted_work(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
(repo / "c.py").write_text("z = 3\n")
report = build_manifest("acp:B", repo, base, shared_with=2)
assert report["status"] == "shared_workspace"
assert "2 other Raven-Code sessions" in report["blockers"][0]
assert report["blockers"][1] == "uncommitted changes must be committed"
def test_a_lone_session_is_attributed_the_work(tmp_path):
repo = tmp_path / "repo"
base = make_repo(repo)
(repo / "b.py").write_text("y = 2\n")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "own commit")
report = build_manifest("acp:A", repo, base)
assert (report["attribution"], report["sharedWith"]) == ("session", 0)
assert report["status"] == "ready_for_integration"
assert report["readyForIntegration"] is True
def test_an_unreadable_tree_stays_unknown_even_when_shared(tmp_path):
"""``unknown`` outranks ``shared_workspace``: a report that could not read
git says so first, and is never ready either way."""
plain = tmp_path / "plain"
plain.mkdir()
report = build_manifest("acp:B", plain, None, shared_with=1)
assert report["attribution"] == "shared"
assert report["readyForIntegration"] is False
@pytest.mark.asyncio
async def test_the_idle_session_does_not_claim_its_peers_commit(tmp_path):
"""Through the hook and the ledger: A and B start on one checkout, A
commits during its turn, B's report at its send is shared and unready --
and so is A's, because from A's side too the tree may hold B's work."""
repo = tmp_path / "repo"
make_repo(repo)
hook = CodeFlowHook(ledger=SessionLedger())
with workdir.bind(repo):
await hook.before_user_inbound(AgentHookContext(session_key="acp:A", inbound_content="add b"))
await hook.before_user_inbound(AgentHookContext(session_key="acp:B", inbound_content="look around"))
(repo / "b.py").write_text("y = 2\n")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "add b")
meta_a: dict = {}
await hook.after_send(AgentHookContext(session_key="acp:A", outbound_content="done", metadata=meta_a))
meta_b: dict = {}
await hook.after_send(AgentHookContext(session_key="acp:B", outbound_content="nothing", metadata=meta_b))
report_b = meta_b["observers"]["acp_meta"][MANIFEST_META_KEY]
assert report_b["commitsPastBase"]["count"] == 1
assert report_b["status"] == "shared_workspace"
assert report_b["readyForIntegration"] is False
assert (report_b["attribution"], report_b["sharedWith"]) == ("shared", 1)
report_a = meta_a["observers"]["acp_meta"][MANIFEST_META_KEY]
assert report_a["status"] == "shared_workspace"
assert report_a["readyForIntegration"] is False