feat: v0.6.0 — read build_stream log instead of dropping it (#2)
DSM emits a readable plaintext build log over the build_stream HTTP body (one short status line per step) and closes the connection when the build is done. The 0.2.5 implementation sent the request and dropped the body unread, leaving users with nothing more than a BUILD_FAILED polling status and no actionable diagnostic. DsmClient.trigger_build_stream now consumes the body line-by-line and returns the collected log as a string. Wall-clock budget of 210 s (under the Claude Desktop ~4 min ceiling); on timeout the partial log is returned with a "[build_stream: timeout — stream still open server-side]" marker so callers know the build continues server-side. Per-chunk ReadTimeout is treated the same way. JSON error envelope, transport-error mapping (M-4), and SID-scrubbed HTTP-error formatting are unchanged. redeploy_project and create_project now parse the returned log via _parse_build_stream_log (any line containing "Error response from daemon:" or ending in " Error" counts as a failure). On a failed log the tools abort immediately, surface the daemon line(s) in the result (e.g. "Error response from daemon: manifest for nginx:9.9.9 not found: manifest unknown"), and skip the polling step. The BUILD_FAILED polling guard (M-5) stays as a second safety net for late failures where the stream was clean but the container exited after start. No new MCP tool: the build log is a live stream and cannot be re-fetched after the build ends, so it is surfaced during redeploy_project / create_project rather than exposed as a standalone get_project_build_log call. Minor version bump because redeploy_project and create_project return materially different strings on a failed build and exit earlier in the failure path. Signatures unchanged. Tests: streamed-log collection, daemon-error log, header ReadTimeout marker, per-chunk ReadTimeout partial log, wall-clock budget truncation, _parse_build_stream_log unit tests, redeploy/create end- to-end behavior with a failing log. Closes #2 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
+110
-17
@@ -6,8 +6,8 @@ Covers the critical paths of DsmClient:
|
||||
sensitive-param log masking.
|
||||
- Session re-auth retry: single-retry semantics, thundering-herd,
|
||||
auth-manager-absent, re-auth failure.
|
||||
- trigger_build_stream: SSE fire-and-forget, JSON error detection,
|
||||
ReadTimeout swallowing, HTTP-error scrubbing.
|
||||
- trigger_build_stream: streamed log collection, JSON error detection,
|
||||
ReadTimeout marker, HTTP-error scrubbing.
|
||||
- upload_text / download_text happy-path + error-response.
|
||||
- _ensure_initialized double-checked-locking and M4 negative-cache cooldown.
|
||||
|
||||
@@ -490,27 +490,65 @@ async def test_request_reauth_thundering_herd_login_called_once() -> None:
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
async def _aiter_from_lines(lines: list[str]):
|
||||
"""Async iterator that yields the given strings (httpx aiter_lines shape)."""
|
||||
for line in lines:
|
||||
yield line
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_sse_fire_and_forget_does_not_read_body() -> None:
|
||||
async def test_build_stream_collects_streamed_log_lines() -> None:
|
||||
"""The streamed plaintext body is consumed line-by-line and returned."""
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
|
||||
resp = make_response(
|
||||
{"success": True},
|
||||
content_type="text/event-stream",
|
||||
{"placeholder": True},
|
||||
content_type="text/html",
|
||||
)
|
||||
resp.aiter_lines = lambda: _aiter_from_lines(
|
||||
[
|
||||
"Container vault Running",
|
||||
"Container vault-db Running",
|
||||
"",
|
||||
]
|
||||
)
|
||||
|
||||
# If the code ever tries to read the SSE body, blow up the test.
|
||||
def _boom(*_a: Any, **_k: Any):
|
||||
raise AssertionError("SSE body must not be read")
|
||||
|
||||
resp.aiter_bytes = MagicMock(side_effect=_boom)
|
||||
client._http.stream = MagicMock(return_value=make_stream_ctx(resp))
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert result is None
|
||||
assert isinstance(result, str)
|
||||
assert "Container vault Running" in result
|
||||
assert "Container vault-db Running" in result
|
||||
# Empty lines are dropped.
|
||||
assert "\n\n" not in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_collects_failure_log_with_daemon_error() -> None:
|
||||
"""Failure log (svc Error + daemon-error line) is returned verbatim."""
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
|
||||
resp = make_response({"placeholder": True}, content_type="text/html")
|
||||
resp.aiter_lines = lambda: _aiter_from_lines(
|
||||
[
|
||||
"nginx Error",
|
||||
(
|
||||
"Error response from daemon: manifest for nginx:9.9.9-nonexistent "
|
||||
"not found: manifest unknown"
|
||||
),
|
||||
]
|
||||
)
|
||||
client._http.stream = MagicMock(return_value=make_stream_ctx(resp))
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert "nginx Error" in result
|
||||
assert "Error response from daemon:" in result
|
||||
assert "manifest unknown" in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -532,6 +570,7 @@ async def test_build_stream_json_error_raises_synology_error() -> None:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_json_success_accepted() -> None:
|
||||
"""JSON envelope with success=true → no streamed log, returns empty string."""
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
@@ -543,11 +582,12 @@ async def test_build_stream_json_success_accepted() -> None:
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert result is None
|
||||
assert result == ""
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_read_timeout_swallowed() -> None:
|
||||
async def test_build_stream_read_timeout_returns_marker() -> None:
|
||||
"""Header-arrival ReadTimeout returns the timeout marker (build still running)."""
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
@@ -560,7 +600,60 @@ async def test_build_stream_read_timeout_swallowed() -> None:
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert result is None
|
||||
assert result == DsmClient.BUILD_STREAM_TIMEOUT_MARKER
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_per_chunk_read_timeout_returns_partial_log() -> None:
|
||||
"""If a streamed chunk times out mid-build, return the partial log + marker."""
|
||||
|
||||
async def aiter_partial_then_timeout():
|
||||
yield "Container vault Pulling"
|
||||
raise httpx.ReadTimeout("chunk timed out")
|
||||
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
|
||||
resp = make_response({"placeholder": True}, content_type="text/html")
|
||||
resp.aiter_lines = aiter_partial_then_timeout
|
||||
client._http.stream = MagicMock(return_value=make_stream_ctx(resp))
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert "Container vault Pulling" in result
|
||||
assert DsmClient.BUILD_STREAM_TIMEOUT_MARKER in result
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_wallclock_budget_breaks_loop(monkeypatch) -> None:
|
||||
"""When the wall-clock budget is exhausted, the loop returns partial log + marker."""
|
||||
import mcp_synology_container.dsm_client as dsm_mod
|
||||
|
||||
# Force the budget to a value just below the initial loop.time() so the
|
||||
# check after the first emitted line trips immediately.
|
||||
monkeypatch.setattr(DsmClient, "BUILD_STREAM_BUDGET", -1.0)
|
||||
|
||||
async def aiter_many():
|
||||
yield "Container vault Pulling"
|
||||
yield "Container vault Running" # Should NOT appear in the result.
|
||||
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
|
||||
resp = make_response({"placeholder": True}, content_type="text/html")
|
||||
resp.aiter_lines = aiter_many
|
||||
client._http.stream = MagicMock(return_value=make_stream_ctx(resp))
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert "Container vault Pulling" in result
|
||||
assert "Container vault Running" not in result
|
||||
assert DsmClient.BUILD_STREAM_TIMEOUT_MARKER in result
|
||||
|
||||
# Keep dsm_mod referenced so the test doesn't trip an unused-import warning.
|
||||
assert dsm_mod.DsmClient is DsmClient
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@@ -628,7 +721,7 @@ async def test_build_stream_http_500_scrubs_sid() -> None:
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_build_stream_malformed_json_body_treated_as_accepted() -> None:
|
||||
"""Defensive branch: JSON content-type but body fails to parse → return None."""
|
||||
"""Defensive branch: JSON content-type but body fails to parse → empty string."""
|
||||
async with DsmClient(base_url="https://nas.local:443") as client:
|
||||
mark_initialized(client)
|
||||
client._http = AsyncMock()
|
||||
@@ -639,7 +732,7 @@ async def test_build_stream_malformed_json_body_treated_as_accepted() -> None:
|
||||
|
||||
result = await client.trigger_build_stream("proj-1")
|
||||
|
||||
assert result is None
|
||||
assert result == ""
|
||||
|
||||
|
||||
# ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user