perf(agent): trim workspace access context without changing scope

This commit is contained in:
Xubin Ren committed 2026-10-11 17:06:39 +08:00
1 parent da02daaa1d
commit a27f294f52
4 files changed
+15 -15

No files matched your search

+4 -4
View File
@@ -190,15 +190,15 @@ class AgentRuntimeControl:
lines = [ lines = [
f"Current project (JSON path): {root}", f"Current project (JSON path): {root}",
( (
"File access: limited to this project, plus tool-specific allowed paths." "File access: project and tool-specific allowed paths only."
if status.restrict_to_workspace if status.restrict_to_workspace
else "File access: not restricted to this project." else "File access: no workspace restriction."
), ),
"OS permissions and tool-specific rules still apply.", "OS permissions and tool rules still apply.",
] ]
if self.__target.exec_config.sandbox: if self.__target.exec_config.sandbox:
lines.append( lines.append(
"A shell sandbox is configured. Extra shell binds do not grant file-tool access." "Shell sandbox configured; extra binds do not grant file-tool access."
) )
return RuntimeContextBlock(source="workspace_access", content=wrap_runtime_context_lines(lines)) return RuntimeContextBlock(source="workspace_access", content=wrap_runtime_context_lines(lines))
@@ -77,7 +77,7 @@ async def test_runner_pressure_commits_summary_and_current_delta(tmp_path) -> No
assert "Current checkpoint." in model_request[0]["content"] assert "Current checkpoint." in model_request[0]["content"]
assert [message["role"] for message in model_request] == ["system", "user"] assert [message["role"] for message in model_request] == ["system", "user"]
assert model_request[1]["content"].startswith("continue the task\n\n") assert model_request[1]["content"].startswith("continue the task\n\n")
assert "File access: not restricted to this project." in model_request[1]["content"] assert "File access: no workspace restriction." in model_request[1]["content"]
reloaded = loop.sessions.get_or_create("cli:test") reloaded = loop.sessions.get_or_create("cli:test")
assert reloaded.messages[0]["content"] == "old-user-0" assert reloaded.messages[0]["content"] == "old-user-0"
@@ -133,7 +133,7 @@ async def test_ephemeral_runner_pressure_summarizes_without_persisting(tmp_path)
model_request = loop.provider.chat_stream_with_retry.await_args_list[1].kwargs["messages"] model_request = loop.provider.chat_stream_with_retry.await_args_list[1].kwargs["messages"]
assert "Transient checkpoint." in model_request[0]["content"] assert "Transient checkpoint." in model_request[0]["content"]
assert model_request[1]["content"].startswith("continue the task\n\n") assert model_request[1]["content"].startswith("continue the task\n\n")
assert "File access: not restricted to this project." in model_request[1]["content"] assert "File access: no workspace restriction." in model_request[1]["content"]
reloaded = loop.sessions.get_or_create("cli:ephemeral") reloaded = loop.sessions.get_or_create("cli:ephemeral")
assert reloaded.messages[: len(original_messages)] == original_messages assert reloaded.messages[: len(original_messages)] == original_messages
+8 -8
View File
@@ -403,7 +403,7 @@ async def test_workspace_inspection_reaches_model_from_selected_chat(tmp_path: P
first_messages = calls[0].kwargs["messages"] first_messages = calls[0].kwargs["messages"]
current = first_messages[-1]["content"] current = first_messages[-1]["content"]
assert f"Current project (JSON path): {json.dumps(str(project.resolve()))}" in current assert f"Current project (JSON path): {json.dumps(str(project.resolve()))}" in current
assert "File access: limited to this project" in current assert "File access: project and tool-specific allowed paths only." in current
assert calls[1].kwargs["messages"][:len(first_messages)] == first_messages assert calls[1].kwargs["messages"][:len(first_messages)] == first_messages
results = { results = {
msg["tool_call_id"]: msg["content"] for msg in calls[1].kwargs["messages"] msg["tool_call_id"]: msg["content"] for msg in calls[1].kwargs["messages"]
@@ -438,8 +438,8 @@ async def test_workspace_context_uses_request_scope_not_active_tool_scope(
block = await control.workspace_context(RequestContext("websocket", "chat", workspace_scope=scope)) block = await control.workspace_context(RequestContext("websocket", "chat", workspace_scope=scope))
assert json.dumps(str(scope.project_path)) in block.content assert json.dumps(str(scope.project_path)) in block.content
assert "old-project" not in block.content assert "old-project" not in block.content
assert ("File access: limited" in block.content) is restricted assert ("File access: project and tool-specific allowed paths only." in block.content) is restricted
assert ("shell sandbox is configured" in block.content) is bool(sandbox) assert ("Shell sandbox configured" in block.content) is bool(sandbox)
assert "OS permissions" in block.content assert "OS permissions" in block.content
assert "system-enforced" not in block.content assert "system-enforced" not in block.content
finally: finally:
@@ -466,7 +466,7 @@ async def test_workspace_context_does_not_require_my_tool(tmp_path: Path) -> Non
provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done")) provider.chat_stream_with_retry = AsyncMock(return_value=LLMResponse(content="done"))
await loop.process_direct("Read the current project", ephemeral=True) await loop.process_direct("Read the current project", ephemeral=True)
message = provider.chat_stream_with_retry.await_args.kwargs["messages"][-1] message = provider.chat_stream_with_retry.await_args.kwargs["messages"][-1]
assert "File access: not restricted to this project." in message["content"] assert "File access: no workspace restriction." in message["content"]
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -489,8 +489,8 @@ async def test_access_changes_refresh_only_new_message_and_preserve_prefix(tmp_p
first, second, third = [LLMProvider._sanitize_empty_content(call.kwargs["messages"]) for call in calls] first, second, third = [LLMProvider._sanitize_empty_content(call.kwargs["messages"]) for call in calls]
# Access changes belong to the new request, not a rewrite of the cached history. # Access changes belong to the new request, not a rewrite of the cached history.
assert json.dumps(second[:len(first)]) == json.dumps(first) assert json.dumps(second[:len(first)]) == json.dumps(first)
assert "File access: limited" in first[-1]["content"] assert "File access: project and tool-specific allowed paths only." in first[-1]["content"]
assert "File access: not restricted" in second[-1]["content"] assert "File access: no workspace restriction." in second[-1]["content"]
assert json.dumps(str(other.resolve())) in third[-1]["content"] assert json.dumps(str(other.resolve())) in third[-1]["content"]
assert json.dumps(str(project.resolve())) not in third[-1]["content"] assert json.dumps(str(project.resolve())) not in third[-1]["content"]
assert all(call.kwargs["tools"] == calls[0].kwargs["tools"] for call in calls) assert all(call.kwargs["tools"] == calls[0].kwargs["tools"] for call in calls)
@@ -531,7 +531,7 @@ async def test_injected_channel_input_reports_active_tool_scope(tmp_path: Path)
calls = loop.provider.chat_stream_with_retry.await_args_list calls = loop.provider.chat_stream_with_retry.await_args_list
context = calls[0].kwargs["messages"][-1]["content"] context = calls[0].kwargs["messages"][-1]["content"]
assert json.dumps(str(project.resolve())) in context assert json.dumps(str(project.resolve())) in context
assert "File access: limited" in context assert "File access: project and tool-specific allowed paths only." in context
assert any(message.get("role") == "tool" and "active project" in message["content"] assert any(message.get("role") == "tool" and "active project" in message["content"]
for message in calls[1].kwargs["messages"]) for message in calls[1].kwargs["messages"])
@@ -594,7 +594,7 @@ async def test_workspace_context_compaction_reuses_serialized_prefix(
await loop.process_direct("Check again", session_key=session.key) await loop.process_direct("Check again", session_key=session.key)
current = provider.chat_stream_with_retry.await_args.kwargs["messages"][-1]["content"] current = provider.chat_stream_with_retry.await_args.kwargs["messages"][-1]["content"]
assert current.count("Current project (JSON path):") == 1 assert current.count("Current project (JSON path):") == 1
assert "File access: not restricted to this project." in current assert "File access: no workspace restriction." in current
@pytest.mark.asyncio @pytest.mark.asyncio
+1 -1
View File
@@ -141,7 +141,7 @@ async def test_compacted_session_waits_for_new_input_without_continuation(
} }
assert [message["role"] for message in sent] == ["system", "user"] assert [message["role"] for message in sent] == ["system", "user"]
assert sent[1]["content"].startswith("hi\n\n") assert sent[1]["content"].startswith("hi\n\n")
assert "File access: not restricted to this project." in sent[1]["content"] assert "File access: no workspace restriction." in sent[1]["content"]
loop.sessions.invalidate(key) loop.sessions.invalidate(key)
resumed = loop.sessions.get_or_create(key) resumed = loop.sessions.get_or_create(key)