flowent 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/pyproject.toml +1 -1
- package/backend/src/flowent/__pycache__/__init__.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/_version.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/agent.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/approval.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/channels.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/cli.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/compact.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/context.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/llm.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/logging.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/main.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/mcp.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/mcp_import.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/patch.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/paths.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/permissions.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/sandbox.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/skills.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/storage.cpython-313.pyc +0 -0
- package/backend/src/flowent/__pycache__/tools.cpython-313.pyc +0 -0
- package/backend/src/flowent/agent.py +23 -1
- package/backend/src/flowent/approval.py +148 -0
- package/backend/src/flowent/cli.py +4 -2
- package/backend/src/flowent/context.py +19 -1
- package/backend/src/flowent/llm.py +51 -11
- package/backend/src/flowent/logging.py +60 -0
- package/backend/src/flowent/main.py +639 -210
- package/backend/src/flowent/patch.py +55 -31
- package/backend/src/flowent/permissions.py +185 -42
- package/backend/src/flowent/sandbox.py +55 -1
- package/backend/src/flowent/static/assets/index-Cl20cARb.css +2 -0
- package/backend/src/flowent/static/assets/index-dsDDsEym.js +81 -0
- package/backend/src/flowent/static/index.html +2 -2
- package/backend/src/flowent/storage.py +113 -18
- package/backend/tests/__pycache__/conftest.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_agent_tools.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_approval.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_channels.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_health.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_llm_providers.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_logging.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_mcp.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_patch.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_permissions.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_persistence.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_skills.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_startup_requirements.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/__pycache__/test_workspace_chat.cpython-313-pytest-9.0.3.pyc +0 -0
- package/backend/tests/test_agent_tools.py +77 -1
- package/backend/tests/test_approval.py +283 -0
- package/backend/tests/test_llm_providers.py +216 -0
- package/backend/tests/test_logging.py +30 -0
- package/backend/tests/test_patch.py +112 -0
- package/backend/tests/test_permissions.py +198 -53
- package/backend/tests/test_persistence.py +78 -0
- package/backend/tests/test_startup_requirements.py +54 -0
- package/backend/tests/test_workspace_chat.py +855 -41
- package/backend/uv.lock +1 -1
- package/dist/frontend/assets/index-Cl20cARb.css +2 -0
- package/dist/frontend/assets/index-dsDDsEym.js +81 -0
- package/dist/frontend/index.html +2 -2
- package/package.json +1 -1
- package/backend/src/flowent/static/assets/index-BREidonU.css +0 -2
- package/backend/src/flowent/static/assets/index-DSniOrhL.js +0 -81
- package/dist/frontend/assets/index-BREidonU.css +0 -2
- package/dist/frontend/assets/index-DSniOrhL.js +0 -81
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import pytest
|
|
4
|
+
|
|
5
|
+
from flowent.approval import (
|
|
6
|
+
ApprovalReviewRequest,
|
|
7
|
+
ApprovalTranscriptEntry,
|
|
8
|
+
review_approval_request,
|
|
9
|
+
)
|
|
10
|
+
from flowent.llm import ProviderConnection, ProviderFormat
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def provider_connection() -> ProviderConnection:
|
|
14
|
+
return ProviderConnection(
|
|
15
|
+
model="model",
|
|
16
|
+
name="Provider",
|
|
17
|
+
provider=ProviderFormat.OPENAI,
|
|
18
|
+
secret_reference="secret",
|
|
19
|
+
)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
@pytest.mark.anyio
|
|
23
|
+
async def test_review_payload_includes_current_user_request_and_transcript(
|
|
24
|
+
tmp_path,
|
|
25
|
+
) -> None:
|
|
26
|
+
captured_messages: list[dict[str, object]] = []
|
|
27
|
+
|
|
28
|
+
async def fake_completion(**request: object) -> object:
|
|
29
|
+
captured_messages.extend(request["messages"])
|
|
30
|
+
return {
|
|
31
|
+
"choices": [
|
|
32
|
+
{
|
|
33
|
+
"message": {
|
|
34
|
+
"content": json.dumps(
|
|
35
|
+
{
|
|
36
|
+
"risk_level": "low",
|
|
37
|
+
"risk_score": 25,
|
|
38
|
+
"rationale": "User approved after concrete risk context.",
|
|
39
|
+
"evidence": [
|
|
40
|
+
{
|
|
41
|
+
"message": "Assistant explained Docker socket impact.",
|
|
42
|
+
"why": "Establishes informed consent.",
|
|
43
|
+
}
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
),
|
|
47
|
+
"role": "assistant",
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
decision = await review_approval_request(
|
|
54
|
+
provider_connection(),
|
|
55
|
+
ApprovalReviewRequest(
|
|
56
|
+
action="additional_permissions",
|
|
57
|
+
arguments={"command": "docker compose up -d --build"},
|
|
58
|
+
cwd=tmp_path,
|
|
59
|
+
tool_name="shell_command",
|
|
60
|
+
user_request="确认",
|
|
61
|
+
transcript=[
|
|
62
|
+
ApprovalTranscriptEntry(
|
|
63
|
+
role="assistant",
|
|
64
|
+
content=(
|
|
65
|
+
"This will recreate the dev container, write to the Docker "
|
|
66
|
+
"socket, and briefly interrupt the local service."
|
|
67
|
+
),
|
|
68
|
+
),
|
|
69
|
+
ApprovalTranscriptEntry(role="user", content="确认"),
|
|
70
|
+
],
|
|
71
|
+
write_paths=[tmp_path / "docker.sock"],
|
|
72
|
+
),
|
|
73
|
+
completion=fake_completion,
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
assert decision.decision == "approved"
|
|
77
|
+
assert decision.risk_level == "low"
|
|
78
|
+
assert decision.risk_score == 25
|
|
79
|
+
assert "informed of the concrete risk" in str(captured_messages[0]["content"])
|
|
80
|
+
payload = json.loads(str(captured_messages[-1]["content"]))
|
|
81
|
+
assert payload["user_request"] == "确认"
|
|
82
|
+
assert payload["transcript"][-1] == {"role": "user", "content": "确认"}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
@pytest.mark.anyio
|
|
86
|
+
async def test_concrete_docker_socket_confirmation_can_be_approved(tmp_path) -> None:
|
|
87
|
+
async def fake_completion(**request: object) -> object:
|
|
88
|
+
return {
|
|
89
|
+
"choices": [
|
|
90
|
+
{
|
|
91
|
+
"message": {
|
|
92
|
+
"content": json.dumps(
|
|
93
|
+
{
|
|
94
|
+
"risk_level": "medium",
|
|
95
|
+
"risk_score": 55,
|
|
96
|
+
"rationale": (
|
|
97
|
+
"The user approved after being told the command "
|
|
98
|
+
"will recreate the dev container through Docker."
|
|
99
|
+
),
|
|
100
|
+
"evidence": [],
|
|
101
|
+
}
|
|
102
|
+
),
|
|
103
|
+
"role": "assistant",
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
decision = await review_approval_request(
|
|
110
|
+
provider_connection(),
|
|
111
|
+
ApprovalReviewRequest(
|
|
112
|
+
action="additional_permissions",
|
|
113
|
+
arguments={
|
|
114
|
+
"command": "docker compose up -d --force-recreate flowent",
|
|
115
|
+
},
|
|
116
|
+
cwd=tmp_path,
|
|
117
|
+
tool_name="shell_command",
|
|
118
|
+
user_request="确认",
|
|
119
|
+
transcript=[
|
|
120
|
+
ApprovalTranscriptEntry(
|
|
121
|
+
role="assistant",
|
|
122
|
+
content=(
|
|
123
|
+
"This will recreate the Flowent dev container through "
|
|
124
|
+
"Docker and may briefly interrupt the running service."
|
|
125
|
+
),
|
|
126
|
+
),
|
|
127
|
+
ApprovalTranscriptEntry(role="user", content="确认"),
|
|
128
|
+
],
|
|
129
|
+
write_paths=[tmp_path / "docker.sock"],
|
|
130
|
+
),
|
|
131
|
+
completion=fake_completion,
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
assert decision.decision == "approved"
|
|
135
|
+
assert decision.risk_level == "medium"
|
|
136
|
+
assert decision.risk_score == 55
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
@pytest.mark.anyio
|
|
140
|
+
async def test_vague_confirmation_without_concrete_risk_context_is_denied(
|
|
141
|
+
tmp_path,
|
|
142
|
+
) -> None:
|
|
143
|
+
captured_payload: dict[str, object] = {}
|
|
144
|
+
|
|
145
|
+
async def fake_completion(**request: object) -> object:
|
|
146
|
+
captured_payload.update(json.loads(str(request["messages"][-1]["content"])))
|
|
147
|
+
return {
|
|
148
|
+
"choices": [
|
|
149
|
+
{
|
|
150
|
+
"message": {
|
|
151
|
+
"content": json.dumps(
|
|
152
|
+
{
|
|
153
|
+
"risk_level": "high",
|
|
154
|
+
"risk_score": 82,
|
|
155
|
+
"rationale": (
|
|
156
|
+
"The transcript only contains a vague confirmation "
|
|
157
|
+
"and no concrete Docker risk explanation."
|
|
158
|
+
),
|
|
159
|
+
"evidence": [],
|
|
160
|
+
}
|
|
161
|
+
),
|
|
162
|
+
"role": "assistant",
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
decision = await review_approval_request(
|
|
169
|
+
provider_connection(),
|
|
170
|
+
ApprovalReviewRequest(
|
|
171
|
+
action="additional_permissions",
|
|
172
|
+
arguments={
|
|
173
|
+
"command": "docker compose up -d --force-recreate flowent",
|
|
174
|
+
},
|
|
175
|
+
cwd=tmp_path,
|
|
176
|
+
tool_name="shell_command",
|
|
177
|
+
user_request="确认",
|
|
178
|
+
transcript=[ApprovalTranscriptEntry(role="user", content="确认")],
|
|
179
|
+
write_paths=[tmp_path / "docker.sock"],
|
|
180
|
+
),
|
|
181
|
+
completion=fake_completion,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
assert decision.decision == "denied"
|
|
185
|
+
assert decision.risk_level == "high"
|
|
186
|
+
assert decision.risk_score == 82
|
|
187
|
+
assert captured_payload["transcript"] == [{"role": "user", "content": "确认"}]
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
@pytest.mark.anyio
|
|
191
|
+
async def test_broad_destructive_action_with_vague_confirmation_is_denied(
|
|
192
|
+
tmp_path,
|
|
193
|
+
) -> None:
|
|
194
|
+
async def fake_completion(**request: object) -> object:
|
|
195
|
+
return {
|
|
196
|
+
"choices": [
|
|
197
|
+
{
|
|
198
|
+
"message": {
|
|
199
|
+
"content": json.dumps(
|
|
200
|
+
{
|
|
201
|
+
"risk_level": "high",
|
|
202
|
+
"risk_score": 96,
|
|
203
|
+
"rationale": (
|
|
204
|
+
"The action can delete broad data and the user "
|
|
205
|
+
"did not approve that concrete destructive risk."
|
|
206
|
+
),
|
|
207
|
+
"evidence": [
|
|
208
|
+
{
|
|
209
|
+
"message": "rm -rf /var/lib/postgresql",
|
|
210
|
+
"why": "Broad destructive write outside the task.",
|
|
211
|
+
}
|
|
212
|
+
],
|
|
213
|
+
}
|
|
214
|
+
),
|
|
215
|
+
"role": "assistant",
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
]
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
decision = await review_approval_request(
|
|
222
|
+
provider_connection(),
|
|
223
|
+
ApprovalReviewRequest(
|
|
224
|
+
action="sandbox_failure",
|
|
225
|
+
arguments={"command": "rm -rf /var/lib/postgresql"},
|
|
226
|
+
cwd=tmp_path,
|
|
227
|
+
tool_name="shell_command",
|
|
228
|
+
tool_result="Read-only file system",
|
|
229
|
+
user_request="确认",
|
|
230
|
+
transcript=[ApprovalTranscriptEntry(role="user", content="确认")],
|
|
231
|
+
),
|
|
232
|
+
completion=fake_completion,
|
|
233
|
+
)
|
|
234
|
+
|
|
235
|
+
assert decision.decision == "denied"
|
|
236
|
+
assert decision.risk_level == "high"
|
|
237
|
+
assert decision.risk_score == 96
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
@pytest.mark.anyio
|
|
241
|
+
async def test_invalid_reviewer_json_is_denied(tmp_path) -> None:
|
|
242
|
+
async def fake_completion(**request: object) -> object:
|
|
243
|
+
return {
|
|
244
|
+
"choices": [
|
|
245
|
+
{"message": {"content": "approved", "role": "assistant"}},
|
|
246
|
+
],
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
decision = await review_approval_request(
|
|
250
|
+
provider_connection(),
|
|
251
|
+
ApprovalReviewRequest(
|
|
252
|
+
action="sandbox_failure",
|
|
253
|
+
arguments={"command": "touch file.txt"},
|
|
254
|
+
cwd=tmp_path,
|
|
255
|
+
tool_name="shell_command",
|
|
256
|
+
tool_result="Read-only file system",
|
|
257
|
+
),
|
|
258
|
+
completion=fake_completion,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
assert decision.decision == "denied"
|
|
262
|
+
assert "valid JSON" in decision.reason
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
@pytest.mark.anyio
|
|
266
|
+
async def test_reviewer_call_failure_is_denied(tmp_path) -> None:
|
|
267
|
+
async def fake_completion(**request: object) -> object:
|
|
268
|
+
raise RuntimeError("model unavailable")
|
|
269
|
+
|
|
270
|
+
decision = await review_approval_request(
|
|
271
|
+
provider_connection(),
|
|
272
|
+
ApprovalReviewRequest(
|
|
273
|
+
action="edit",
|
|
274
|
+
arguments={"patch": "*** Begin Patch\n*** End Patch"},
|
|
275
|
+
cwd=tmp_path,
|
|
276
|
+
tool_name="apply_patch",
|
|
277
|
+
write_paths=[tmp_path / "outside"],
|
|
278
|
+
),
|
|
279
|
+
completion=fake_completion,
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
assert decision.decision == "denied"
|
|
283
|
+
assert "model unavailable" in decision.reason
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
1
3
|
import pytest
|
|
2
4
|
|
|
3
5
|
from flowent.llm import (
|
|
@@ -8,10 +10,17 @@ from flowent.llm import (
|
|
|
8
10
|
build_litellm_request,
|
|
9
11
|
chunk_delta_reasoning,
|
|
10
12
|
complete_chat,
|
|
13
|
+
normalize_system_messages,
|
|
11
14
|
stream_chat,
|
|
12
15
|
)
|
|
13
16
|
|
|
14
17
|
|
|
18
|
+
def read_single_llm_request_diagnostic(tmp_path):
|
|
19
|
+
files = sorted((tmp_path / "logs" / "llm-requests").glob("llm-request-*.json"))
|
|
20
|
+
assert len(files) == 1
|
|
21
|
+
return json.loads(files[0].read_text())
|
|
22
|
+
|
|
23
|
+
|
|
15
24
|
def test_supported_provider_formats_match_product_choices() -> None:
|
|
16
25
|
assert [provider.value for provider in ProviderFormat] == [
|
|
17
26
|
"openai",
|
|
@@ -137,6 +146,63 @@ async def test_complete_chat_uses_injected_litellm_completion() -> None:
|
|
|
137
146
|
assert answer == ChatMessage(role="assistant", content="Here is the checklist.")
|
|
138
147
|
|
|
139
148
|
|
|
149
|
+
@pytest.mark.anyio
|
|
150
|
+
async def test_development_mode_writes_completion_request_diagnostic_file(
|
|
151
|
+
tmp_path, monkeypatch
|
|
152
|
+
) -> None:
|
|
153
|
+
monkeypatch.setenv("FLOWENT_DATA_DIR", str(tmp_path))
|
|
154
|
+
monkeypatch.setenv("DEBUG", "true")
|
|
155
|
+
|
|
156
|
+
async def fake_completion(**request: object) -> dict[str, object]:
|
|
157
|
+
return {
|
|
158
|
+
"choices": [
|
|
159
|
+
{
|
|
160
|
+
"message": {
|
|
161
|
+
"content": "Here is the checklist.",
|
|
162
|
+
"role": "assistant",
|
|
163
|
+
},
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
connection = ProviderConnection(
|
|
169
|
+
name="Responses",
|
|
170
|
+
provider=ProviderFormat.OPENAI_RESPONSES,
|
|
171
|
+
model="gpt-5.1",
|
|
172
|
+
secret_reference="sk-request-secret",
|
|
173
|
+
)
|
|
174
|
+
messages = [ChatMessage(role="user", content="Create a checklist.")]
|
|
175
|
+
tools = [
|
|
176
|
+
{
|
|
177
|
+
"type": "function",
|
|
178
|
+
"function": {
|
|
179
|
+
"name": "create_checklist",
|
|
180
|
+
"description": "Create a checklist.",
|
|
181
|
+
},
|
|
182
|
+
}
|
|
183
|
+
]
|
|
184
|
+
|
|
185
|
+
await complete_chat(
|
|
186
|
+
connection,
|
|
187
|
+
messages,
|
|
188
|
+
completion=fake_completion,
|
|
189
|
+
tools=tools,
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
diagnostic = read_single_llm_request_diagnostic(tmp_path)
|
|
193
|
+
|
|
194
|
+
assert diagnostic == {
|
|
195
|
+
"base_url": None,
|
|
196
|
+
"litellm_model": "openai/gpt-5.1",
|
|
197
|
+
"messages": [{"content": "Create a checklist.", "role": "user"}],
|
|
198
|
+
"model": "gpt-5.1",
|
|
199
|
+
"provider": "openai_responses",
|
|
200
|
+
"reasoning_effort": "default",
|
|
201
|
+
"stream": False,
|
|
202
|
+
"tools": tools,
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
|
|
140
206
|
@pytest.mark.anyio
|
|
141
207
|
async def test_stream_chat_uses_litellm_streaming() -> None:
|
|
142
208
|
captured_request: dict[str, object] = {}
|
|
@@ -169,3 +235,153 @@ async def test_stream_chat_uses_litellm_streaming() -> None:
|
|
|
169
235
|
assert captured_request["stream"] is True
|
|
170
236
|
assert captured_request["model"] == "openai/gpt-5.1"
|
|
171
237
|
assert chunks == ["Here is ", "the checklist."]
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
@pytest.mark.anyio
|
|
241
|
+
async def test_development_mode_writes_one_streaming_request_diagnostic_file(
|
|
242
|
+
tmp_path, monkeypatch
|
|
243
|
+
) -> None:
|
|
244
|
+
monkeypatch.setenv("FLOWENT_DATA_DIR", str(tmp_path))
|
|
245
|
+
monkeypatch.setenv("DEBUG", "true")
|
|
246
|
+
|
|
247
|
+
async def fake_completion(**request: object) -> object:
|
|
248
|
+
async def chunks() -> object:
|
|
249
|
+
yield {"choices": [{"delta": {"content": "Here is "}}]}
|
|
250
|
+
yield {"choices": [{"delta": {"content": "the checklist."}}]}
|
|
251
|
+
|
|
252
|
+
return chunks()
|
|
253
|
+
|
|
254
|
+
connection = ProviderConnection(
|
|
255
|
+
name="Responses",
|
|
256
|
+
provider=ProviderFormat.OPENAI_RESPONSES,
|
|
257
|
+
model="gpt-5.1",
|
|
258
|
+
secret_reference="sk-request-secret",
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
chunks = [
|
|
262
|
+
chunk
|
|
263
|
+
async for chunk in stream_chat(
|
|
264
|
+
connection,
|
|
265
|
+
[ChatMessage(role="user", content="Create a checklist.")],
|
|
266
|
+
completion=fake_completion,
|
|
267
|
+
)
|
|
268
|
+
]
|
|
269
|
+
diagnostic = read_single_llm_request_diagnostic(tmp_path)
|
|
270
|
+
|
|
271
|
+
assert chunks == ["Here is ", "the checklist."]
|
|
272
|
+
assert diagnostic["stream"] is True
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
@pytest.mark.anyio
|
|
276
|
+
async def test_development_request_diagnostic_omits_api_key_and_secret_values(
|
|
277
|
+
tmp_path, monkeypatch
|
|
278
|
+
) -> None:
|
|
279
|
+
monkeypatch.setenv("FLOWENT_DATA_DIR", str(tmp_path))
|
|
280
|
+
monkeypatch.setenv("DEBUG", "true")
|
|
281
|
+
|
|
282
|
+
async def fake_completion(**request: object) -> dict[str, object]:
|
|
283
|
+
return {
|
|
284
|
+
"choices": [
|
|
285
|
+
{
|
|
286
|
+
"message": {
|
|
287
|
+
"content": "Here is the checklist.",
|
|
288
|
+
"role": "assistant",
|
|
289
|
+
},
|
|
290
|
+
}
|
|
291
|
+
]
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
connection = ProviderConnection(
|
|
295
|
+
name="Responses",
|
|
296
|
+
provider=ProviderFormat.OPENAI_RESPONSES,
|
|
297
|
+
model="gpt-5.1",
|
|
298
|
+
secret_reference="sk-provider-secret",
|
|
299
|
+
)
|
|
300
|
+
tools = [
|
|
301
|
+
{
|
|
302
|
+
"type": "function",
|
|
303
|
+
"function": {
|
|
304
|
+
"name": "create_checklist",
|
|
305
|
+
"description": "Uses api_key=sk-tool-secret when configured.",
|
|
306
|
+
},
|
|
307
|
+
}
|
|
308
|
+
]
|
|
309
|
+
|
|
310
|
+
await complete_chat(
|
|
311
|
+
connection,
|
|
312
|
+
[ChatMessage(role="user", content="authorization=Bearer sk-message-secret")],
|
|
313
|
+
completion=fake_completion,
|
|
314
|
+
tools=tools,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
rendered = next(
|
|
318
|
+
(tmp_path / "logs" / "llm-requests").glob("llm-request-*.json")
|
|
319
|
+
).read_text()
|
|
320
|
+
|
|
321
|
+
assert "api_key" not in rendered
|
|
322
|
+
assert "sk-provider-secret" not in rendered
|
|
323
|
+
assert "sk-tool-secret" not in rendered
|
|
324
|
+
assert "sk-message-secret" not in rendered
|
|
325
|
+
|
|
326
|
+
|
|
327
|
+
@pytest.mark.anyio
|
|
328
|
+
async def test_non_development_mode_skips_request_diagnostic_file(
|
|
329
|
+
tmp_path, monkeypatch
|
|
330
|
+
) -> None:
|
|
331
|
+
monkeypatch.setenv("FLOWENT_DATA_DIR", str(tmp_path))
|
|
332
|
+
monkeypatch.delenv("DEBUG", raising=False)
|
|
333
|
+
|
|
334
|
+
async def fake_completion(**request: object) -> dict[str, object]:
|
|
335
|
+
return {
|
|
336
|
+
"choices": [
|
|
337
|
+
{
|
|
338
|
+
"message": {
|
|
339
|
+
"content": "Here is the checklist.",
|
|
340
|
+
"role": "assistant",
|
|
341
|
+
},
|
|
342
|
+
}
|
|
343
|
+
]
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
connection = ProviderConnection(
|
|
347
|
+
name="Responses",
|
|
348
|
+
provider=ProviderFormat.OPENAI_RESPONSES,
|
|
349
|
+
model="gpt-5.1",
|
|
350
|
+
secret_reference="sk-request-secret",
|
|
351
|
+
)
|
|
352
|
+
|
|
353
|
+
await complete_chat(
|
|
354
|
+
connection,
|
|
355
|
+
[ChatMessage(role="user", content="Create a checklist.")],
|
|
356
|
+
completion=fake_completion,
|
|
357
|
+
)
|
|
358
|
+
|
|
359
|
+
assert not (tmp_path / "logs" / "llm-requests").exists()
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def test_normalize_system_messages_keeps_multiple_system_messages_for_openai() -> None:
|
|
363
|
+
messages = [
|
|
364
|
+
{"role": "system", "content": "Base prompt."},
|
|
365
|
+
{"role": "system", "content": "Configured prompt."},
|
|
366
|
+
{"role": "user", "content": "Hello."},
|
|
367
|
+
]
|
|
368
|
+
|
|
369
|
+
assert normalize_system_messages(messages, ProviderFormat.OPENAI) == messages
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def test_normalize_system_messages_converts_additional_system_messages_for_anthropic() -> (
|
|
373
|
+
None
|
|
374
|
+
):
|
|
375
|
+
messages = [
|
|
376
|
+
{"role": "system", "content": "Base prompt."},
|
|
377
|
+
{"role": "system", "content": "Configured prompt."},
|
|
378
|
+
{"role": "system", "content": "Project prompt."},
|
|
379
|
+
{"role": "user", "content": "Hello."},
|
|
380
|
+
]
|
|
381
|
+
|
|
382
|
+
assert normalize_system_messages(messages, ProviderFormat.ANTHROPIC) == [
|
|
383
|
+
{"role": "system", "content": "Base prompt."},
|
|
384
|
+
{"role": "user", "content": "Configured prompt."},
|
|
385
|
+
{"role": "user", "content": "Project prompt."},
|
|
386
|
+
{"role": "user", "content": "Hello."},
|
|
387
|
+
]
|
|
@@ -8,6 +8,7 @@ from flowent.logging import (
|
|
|
8
8
|
configure_logging,
|
|
9
9
|
ensure_logging_configured,
|
|
10
10
|
redact_log_value,
|
|
11
|
+
sanitize_diagnostic_value,
|
|
11
12
|
)
|
|
12
13
|
|
|
13
14
|
|
|
@@ -103,6 +104,35 @@ def test_logging_redacts_full_api_key_but_keeps_context(tmp_path, monkeypatch) -
|
|
|
103
104
|
)
|
|
104
105
|
|
|
105
106
|
|
|
107
|
+
def test_diagnostic_sanitizer_removes_secret_fields_and_values() -> None:
|
|
108
|
+
sanitized = sanitize_diagnostic_value(
|
|
109
|
+
{
|
|
110
|
+
"api_key": "sk-root-secret",
|
|
111
|
+
"messages": [
|
|
112
|
+
{
|
|
113
|
+
"role": "user",
|
|
114
|
+
"content": "authorization=Bearer sk-message-secret",
|
|
115
|
+
}
|
|
116
|
+
],
|
|
117
|
+
"tools": [
|
|
118
|
+
{
|
|
119
|
+
"function": {
|
|
120
|
+
"name": "send_message",
|
|
121
|
+
"description": "Needs api_key=sk-tool-secret.",
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
],
|
|
125
|
+
}
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
rendered = str(sanitized)
|
|
129
|
+
|
|
130
|
+
assert "api_key" not in rendered
|
|
131
|
+
assert "sk-root-secret" not in rendered
|
|
132
|
+
assert "sk-message-secret" not in rendered
|
|
133
|
+
assert "sk-tool-secret" not in rendered
|
|
134
|
+
|
|
135
|
+
|
|
106
136
|
def test_direct_main_app_import_creates_data_log_file(tmp_path, monkeypatch) -> None:
|
|
107
137
|
monkeypatch.setenv("FLOWENT_DATA_DIR", str(tmp_path))
|
|
108
138
|
sys.modules.pop("flowent.main", None)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from flowent.patch import PatchError, affected_paths, apply_patch
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def test_apply_patch_applies_context_hunk_with_interleaved_changes(tmp_path) -> None:
|
|
7
|
+
target = tmp_path / "notes.txt"
|
|
8
|
+
target.write_text("start\nalpha\nmiddle\nbeta\nend\n")
|
|
9
|
+
patch = """*** Begin Patch
|
|
10
|
+
*** Update File: notes.txt
|
|
11
|
+
@@
|
|
12
|
+
start
|
|
13
|
+
-alpha
|
|
14
|
+
+one
|
|
15
|
+
middle
|
|
16
|
+
-beta
|
|
17
|
+
+two
|
|
18
|
+
end
|
|
19
|
+
*** End Patch
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
result = apply_patch(patch, tmp_path)
|
|
23
|
+
|
|
24
|
+
assert result == {"files": [{"path": str(target), "status": "modified"}]}
|
|
25
|
+
assert target.read_text() == "start\none\nmiddle\ntwo\nend\n"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def test_apply_patch_reports_context_mismatch(tmp_path) -> None:
|
|
29
|
+
target = tmp_path / "notes.txt"
|
|
30
|
+
target.write_text("start\nalpha\nend\n")
|
|
31
|
+
patch = """*** Begin Patch
|
|
32
|
+
*** Update File: notes.txt
|
|
33
|
+
@@
|
|
34
|
+
missing
|
|
35
|
+
-alpha
|
|
36
|
+
+beta
|
|
37
|
+
end
|
|
38
|
+
*** End Patch
|
|
39
|
+
"""
|
|
40
|
+
|
|
41
|
+
with pytest.raises(PatchError, match=r"Patch context was not found\."):
|
|
42
|
+
apply_patch(patch, tmp_path)
|
|
43
|
+
|
|
44
|
+
assert target.read_text() == "start\nalpha\nend\n"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_apply_patch_applies_multiple_hunks_in_order(tmp_path) -> None:
|
|
48
|
+
target = tmp_path / "notes.txt"
|
|
49
|
+
target.write_text("first\nsame\nend first\nsecond\nsame\nend second\n")
|
|
50
|
+
patch = """*** Begin Patch
|
|
51
|
+
*** Update File: notes.txt
|
|
52
|
+
@@
|
|
53
|
+
first
|
|
54
|
+
-same
|
|
55
|
+
+one
|
|
56
|
+
end first
|
|
57
|
+
@@
|
|
58
|
+
second
|
|
59
|
+
-same
|
|
60
|
+
+two
|
|
61
|
+
end second
|
|
62
|
+
*** End Patch
|
|
63
|
+
"""
|
|
64
|
+
|
|
65
|
+
apply_patch(patch, tmp_path)
|
|
66
|
+
|
|
67
|
+
assert target.read_text() == "first\none\nend first\nsecond\ntwo\nend second\n"
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_apply_patch_keeps_simple_contiguous_replacement(tmp_path) -> None:
|
|
71
|
+
target = tmp_path / "notes.txt"
|
|
72
|
+
target.write_text("alpha\nbeta\n")
|
|
73
|
+
patch = """*** Begin Patch
|
|
74
|
+
*** Update File: notes.txt
|
|
75
|
+
@@
|
|
76
|
+
-alpha
|
|
77
|
+
-beta
|
|
78
|
+
+ready
|
|
79
|
+
*** End Patch
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
apply_patch(patch, tmp_path)
|
|
83
|
+
|
|
84
|
+
assert target.read_text() == "ready\n"
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_affected_paths_reads_structured_patch_write_targets(tmp_path) -> None:
|
|
88
|
+
patch = """*** Begin Patch
|
|
89
|
+
*** Update File: notes.txt
|
|
90
|
+
@@
|
|
91
|
+
-alpha
|
|
92
|
+
+beta
|
|
93
|
+
*** Add File: created.txt
|
|
94
|
+
+hello
|
|
95
|
+
*** Delete File: old.txt
|
|
96
|
+
*** Update File: before.txt
|
|
97
|
+
*** Move to: after.txt
|
|
98
|
+
@@
|
|
99
|
+
-before
|
|
100
|
+
+after
|
|
101
|
+
*** End Patch
|
|
102
|
+
"""
|
|
103
|
+
|
|
104
|
+
paths = affected_paths(patch, tmp_path)
|
|
105
|
+
|
|
106
|
+
assert paths == [
|
|
107
|
+
(tmp_path / "notes.txt").resolve(strict=False),
|
|
108
|
+
(tmp_path / "created.txt").resolve(strict=False),
|
|
109
|
+
(tmp_path / "old.txt").resolve(strict=False),
|
|
110
|
+
(tmp_path / "before.txt").resolve(strict=False),
|
|
111
|
+
(tmp_path / "after.txt").resolve(strict=False),
|
|
112
|
+
]
|