Skip to content

Self-improving AI Employees for everyone

test(cli_agent) + docs(CLAUDE.md): unblock 1,175-test suite + reflect ws/auth swap

trohitg1e6506c5b262
diff --git a/CLAUDE.md b/CLAUDE.md
index d7c20c3d..e238cd1f 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -27,6 +27,8 @@ This is a React Flow-based workflow automation platform implementing n8n-inspire
| **[Email Service](./docs-internal/email_service.md)** | IMAP/SMTP integration via Himalaya CLI with EmailService orchestrator, provider presets, custom credential fallback, and polling triggers |
| **[Stripe Service](./docs-internal/stripe_service.md)** | Stripe CLI integration — `stripeAction` (CLI pass-through) + `stripeReceive` (signed-webhook trigger). Reference plugin for the Wave 12 event framework AND the **CLI-managed-auth pattern**: browser OAuth via `stripe login --non-interactive` + `--complete <url>` (URL extracted from `next_step` shell command via `shlex.split`) + auto-installed binary (`ensure_stripe_cli` from GitHub releases) + marker-token reuse of `auth_service.store_oauth_tokens` + CloudEvents-shaped `broadcast_credential_event("credential.oauth.connected" \| ".disconnected", provider=...)` — zero per-provider hardcoding in the frontend. Daemon stdout/stderr ingested via `ProcessService`'s `line_handler` callback (no log-file tailing); credential gate routed through `await self.has_credential()` so non-api-key auth (`is_logged_in()`) plugs in via subclass override. |
| **[CI/CD Pipeline](./docs-internal/ci_cd.md)** | GitHub Actions workflows, predeploy validation, release publishing, and composite setup action |
+| **[Performance](./docs-internal/performance.md)** | Cold-start measurements (Application startup complete 2.90 s warm, first WS connect 8.29 s) + per-phase timeline + optimisation history (lazy LangChain imports, esbuild sidecar bundle, scoped `compileall`, PartySocket reconnect, TanStack Query auth bootstrap, `manualChunks`) + bottleneck inventory + reproduction commands + anti-patterns to never reintroduce. Pairs with the build-time pipeline doc below. |
+| **[Release Build Pipeline](./docs-internal/release_build_pipeline.md)** | npm-distribution build pipeline: `tsgo` for type-check (5× faster than tsc), Vite `manualChunks` + `target: 'es2022'`, esbuild Node.js sidecar bundle (drops `tsx` interpreter cost), `python -O -m compileall` for project-source bytecode (excludes `.venv/`, `tests/`). Single source of truth for the bytecode-compile path list: `machina.commands.build.COMPILEALL_SOURCE_DIRS`. |
| **[Workflow Schema](./docs-internal/workflow-schema.md)** | JSON schema for workflows, edge handle conventions, config node architecture |
| **[Execution Engine Design](./docs-internal/DESIGN.md)** | Architecture patterns, design standards, and implementation details for the workflow execution engine |
| **[Execution Roadmap](./docs-internal/ROADMAP.md)** | Implementation status, completed phases, and pending features |
@@ -5118,19 +5120,16 @@ This function:
- `scripts/docker.js` wrapper auto-adds `--profile redis` when `REDIS_ENABLED=true` in `.env`
- npm scripts (`docker:up`, `docker:down`, etc.) use the wrapper for seamless handling
- Backend no longer depends on Redis - depends only on WhatsApp service
-- **WebSocket Reconnect Fix**: Fixed rapid reconnect loop caused by React effect cleanup
- - Split effects: connect effect, logout effect, unmount cleanup effect
- - Added `isMountedRef` to prevent connections after unmount
- - Added 100ms delay to avoid React Strict Mode double-connection issues
- - WebSocket now stable without 6-9 second reconnect loops
+- **WebSocket Reconnect via PartySocket** (`partysocket/ws` from Cloudflare): native-WS-compatible class handles jittered exponential backoff, message replay (`maxEnqueuedMessages: 200`), and intentional-close (RFC 6455 §7.4.1 code 1000) automatically. Replaces the previous flat 3 s `setTimeout(connect, 3000)` loop. Reconnect envelope (`MIN_DELAY_MS: 250`, `MAX_DELAY_MS: 8000`, `GROW_FACTOR: 1.3`) lives in `client/src/lib/connectionConfig.ts`. The +12 s WS-drop-and-reconnect cycle observed under React Strict Mode is gone — verified across two consecutive cold launches with zero `Client disconnected` events through 130+ s of activity. See `docs-internal/performance.md` for measurements.
- **Docker Backend Fix**: Backend container uses Python uvicorn directly
- Changed from `npm run start` (failed - npm not in Python image) to `python -m uvicorn`
- CMD: `["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "3010"]`
- **Configurable Authentication**: `VITE_AUTH_ENABLED` environment variable
- Set to `false` to bypass login entirely (useful for local development)
- Frontend creates anonymous user with owner privileges when disabled
- - AuthContext includes retry logic (5 retries, exponential backoff) for startup race conditions
+ - AuthContext bootstraps via TanStack Query `useQuery({queryKey: ['auth','status'], retry, retryDelay, signal})` — replaced the previous recursive `setTimeout` chain. Full-jitter exponential backoff per the AWS Architecture Blog formula `random(0, min(CAP_MS, BASE_MS * 2^attempt))` with `BASE_MS=50`, `CAP_MS=4000`, `MAX_ATTEMPTS=7` (constants in `client/src/lib/connectionConfig.ts`). Cumulative budget ~10 s vs. the old 31 s; sub-second granularity early covers the typical 4 s backend cold-start window in 4-5 attempts. 401/403 short-circuit the retry chain (no budget burn for "auth disabled / not logged in" responses). `signal` (AbortController) plumbed through `queryFn` so unmount + Strict Mode cleanup cancel in-flight requests automatically. login / register / logout invalidate the cache via `queryClient.invalidateQueries(AUTH_STATUS_QUERY_KEY)`.
- Pydantic Settings accepts `vite_auth_enabled` field (required due to `extra="forbid"`)
+- **CloudEvents v1.0 envelope on the wire (preserved end-to-end)**: backend `WorkflowEvent` ([server/services/events/envelope.py](./server/services/events/envelope.py)) and frontend `WorkflowEvent<T>` interface ([client/src/types/cloudEvents.ts](./client/src/types/cloudEvents.ts)) mirror each other. `matchesType(event, 'credential.api_key.*')` parity-tested vs `WorkflowEvent.matches_type` Pydantic method. Today wraps inside the legacy `{type, data}` WS frame (e.g. `credential_catalogue_updated` → `data: <envelope>`); future Wave 12 sources (`stripe.*`, `telegram.*`, `task.*`) drop into the same envelope so a single FE switch case routes them via glob dispatch instead of inventing per-source wire keys.
- **whatsapp-rpc Package**: External dependency for WhatsApp integration
- Published to npm as `whatsapp-rpc` (unscoped) and GitHub Packages as `@trohitg/whatsapp-rpc`
- Published to PyPI as `whatsapp-rpc` (async Python client)
diff --git a/server/tests/services/cli_agent/test_service.py b/server/tests/services/cli_agent/test_service.py
index a523ce8b..13eb974c 100644
--- a/server/tests/services/cli_agent/test_service.py
+++ b/server/tests/services/cli_agent/test_service.py
@@ -14,6 +14,7 @@ from __future__ import annotations
import asyncio
import tempfile
from pathlib import Path
+from unittest.mock import MagicMock
import pytest
@@ -183,13 +184,21 @@ def _logger_messages(*module_loggers) -> str:
@pytest.mark.asyncio
-async def test_run_batch_emits_diagnostic_logs_when_workspace_not_git():
+async def test_run_batch_emits_diagnostic_logs_when_workspace_not_git(monkeypatch):
"""Abort path: `run_batch` enters, fails the resolver, returns
structured failure. The `[CC-Agent run_batch] enter` and
`[CC-Agent run_batch] aborting` log lines must fire so the operator
sees WHY the batch never reached `register_batch`."""
- from services.cli_agent.service import logger as svc_logger
- svc_logger.reset_mock()
+ # Force the module-level logger to a Mock for THIS test, regardless of
+ # whether conftest's `core.logging.get_logger` stub was active at the
+ # moment the service module was first imported. In the full-suite
+ # ordering some sibling test imports `core.logging` after the stub is
+ # placed, leaving the cli_agent module with a real structlog
+ # BoundLogger that has no `reset_mock`. Locally patching makes the test
+ # robust to import-order pollution.
+ from services.cli_agent import service as svc_mod
+ svc_logger = MagicMock()
+ monkeypatch.setattr(svc_mod, "logger", svc_logger)
svc = get_ai_cli_service() # DI singleton — same accessor production uses
import tempfile
@@ -226,12 +235,17 @@ async def test_run_batch_registers_mcp_batch_on_happy_path(monkeypatch):
end-to-end and the spawned CLI WOULD see the MCP server registered
for it."""
from services.cli_agent import session as session_mod
- from services.cli_agent.service import logger as svc_logger
- from services.cli_agent.mcp_server import logger as mcp_logger
+ from services.cli_agent import service as svc_mod
+ from services.cli_agent import mcp_server as mcp_mod
from services.cli_agent.protocol import SessionResult
- svc_logger.reset_mock()
- mcp_logger.reset_mock()
+ # See the abort-path test for the rationale — patch the module-level
+ # logger directly so the test does not depend on conftest's
+ # `core.logging.get_logger` stub being active at import time.
+ svc_logger = MagicMock()
+ mcp_logger = MagicMock()
+ monkeypatch.setattr(svc_mod, "logger", svc_logger)
+ monkeypatch.setattr(mcp_mod, "logger", mcp_logger)
started = {"count": 0}