- Add rule 5.3 in AGENTS.md requiring consistent test_<unit>_<behavior> test function names
7.1 KiB
Project Guidelines & Safety Rules for AI Agents
1. Mission & Authority
lg-smtv-mcp is a Python Model Context Protocol (MCP) server for controlling a single pre-paired LG webOS Smart TV over the local network. It runs primarily as a long-lived streamable-http daemon on a homelab server on the same LAN (with stdio supported for local testing), keeping SSAP WebSocket, pointer input socket, subscribe state cache, and optional Developer Mode asyncssh/SFTP connections warm. It stays strictly LLM-free: vision-guided UI navigation lives in the MCP host (such as Antigravity, Claude, or a gemini-decision-1 loop), while the server exposes high-level, decision-model-friendly tools and composite actions (such as batched key sequences with optional inline ImageContent screenshots).
2. Working Tree Authority & Safety (MANDATORY)
The working tree represents the user's active work.
- Inspect First: Run
git statusand list/read target directories/files before creating or modifying anything. - Check for Existing Files: Before creating any new file, verify whether the file (or a user equivalent) already exists on disk. Never overwrite or re-scaffold user work.
- Never Overwrite Blindly: Use available file-reading tools. Preserve user comments, links, and conventions. Prefer surgical edits over wholesale overwrites.
- No Destructive Git Commands: Never run
reset --hard,clean -fd,restore,checkout,stash,rebase, oramendwithout explicit permission. - Incremental Retrospective Commits: Commit each coherent, verified micro-step on a working branch rather than accumulating an entire feature into one large commit. Commits on
mainare allowed for documentation changes (not affecting functionality or architecture). Multi-file commits must include structured-bullets explaining what changed and why per area so the Git history tells a retrospective of the design decisions. - Live Planning & Task Synchronization: Authoritative tracker is
kiskaadee/lg-smtv-mcpongitea.roadtotech.me(Project Board ID6:Project Pipeline), synchronized againstdocs/designs/lg-smtv-mcp.md. Keep Gitea issues, milestones, and board cards aligned with code reality as slices complete; re-rungitea-planon scope shifts or when reaching a milestone Readiness Gate (such as decomposingM2after theM1MVP E2E gate). - Final Diff Verification: Before completing work, run
git statusandgit diffto verify that no unrelated user changes were modified or discarded. Distinguish user changes from agent changes in completion reports.
3. Architecture & Component Contracts (Progressive Triggers)
- Mandatory Architecture Review: Always read
docs/designs/lg-smtv-mcp.mdanddocs/poc/capabilities.md(anddocs/architecture.mdonce created) before changing MCP tool schemas, SSAP/SSH transport boundaries, or directory layouts. - Stop and Ask: If an implementation conflicts with documentation or an ambiguous requirement materially affects architecture, stop and ask the user rather than guessing or silently changing the design.
- No Speculative Robustness: Do not add unrequested fallback layers, complex generic abstractions, or future-proofing bloat.
3.1 Documentation & Diagram Standards
- Contracts Without Code Duplication: Avoid pasting Python class or dataclass blocks into architecture/design docs. Model relationships with a Mermaid
classDiagram(orerDiagram), summarize exact signatures in a concise table, and link directly to the authoritative source file (label as (planned) if not yet created). - Policy vs. Mechanism in Diagrams: Keep flowcharts at the domain-policy level (e.g., "Is the TV in standby (
1008)?") rather than mirroring low-level library syntax. Capture mandatory mechanism invariants in prose or the detailed implementation manifest.
4. Operational & Protocol Invariants
- No Personal Setup Leakage: Never hardcode personal LAN IPs, MAC addresses, SSH host aliases, or local filesystem paths in source code, default configs, tests, or new docs. Treat device coordinates as runtime config (
.env/ env vars / CLI args) and use placeholders (<TV_IP>,<TV_MAC>) in examples. - Live-Hardware Safety & Offline Tests: Never send commands to a physical TV during automated runs or tests without explicit user approval. All automated tests must run offline against mocked SSAP, pointer socket, WoL, and
asyncsshProtocolboundaries. - Standby & Fail-Fast: WebSocket close code
1008(Try Again Later (EWS)) meansTV_OFFLINE_STANDBY—mutating tools must fail fast and never implicitly auto-wake the TV; read-only state queries returnconnected: false, stale: truewithlast_knownvalues. - SSAP & Pointer Socket: Always include
manifest.permissionsinregistereven when reusing a savedclient-key. Route frames bymsg["id"]in a single background reader task; never call ad-hocws.recv()alongsidesubscribe. - Dev Mode SFTP Capture:
asyncsshmust useconfig=[]and explicit"ssh-rsa"algorithms (no+ssh-rsaprefix). Capture to/tmp/lg_smtv_cap_<seq>.jpg, pull into memory only onreturnValue: true, andsftp.remove()infinally:. On compositekeys(..., capture=True), report partial success (keys_sent: true, capture_error: ...) if SSH fails after keystrokes succeed. - PoC Archive & Secrets:
docs/poc/is read-only historical reference—do not modify PoC scripts for new features. Never commit.client_key.json,.env, SSH keys, or*.jpgcaptures.
5. Verification, Hooks & CI Expectations
Every task and commit must pass automated verification inside nix develop:
- Local Pre-Commit Hook (
.githooks/pre-commit): Configured automatically byflake.nixshellHook(git config core.hooksPath .githooks). Runsruff check .,ruff format --check .,pyright, andpytestbefore every commit. Never bypass hooks with--no-verify. - TDD Red-Phase Commits (
@pytest.mark.xfail): When committing Red-phase tests before the feature implementation exists, provide typed symbol stubs (raise NotImplementedError) sopyrightpasses, and decorate unimplemented tests with@pytest.mark.xfail(strict=True, reason="Red phase: ...")sopytestand.githooks/pre-commitexit0while still failing if a Red test passes tautologically. Removexfailin the Green-phase commit. - Behavior-Driven Test Naming: Name all test functions using the consistent
test_<unit>_<behavior>convention (e.g.,test_load_config_rejects_invalid_env_values_with_config_errorrather than broad names liketest_load_config_defaults) sopytest -voutput reads as a clear specification of expected behavior. - Server-Side Gitea Actions CI (
.gitea/workflows/ci.yaml): Runsnix flake checkon every push and pull request targetingmain, executingruff check .,ruff format --check .,pyright,pytestCheckHook, andbuildPythonApplicationinside the isolated Nix sandbox. Verifynix flake checklocally before opening or updating a pull request. - Completion Reporting: Report: (1) What changed, (2) Files modified/created, (3) Verification performed, (4) Unverified areas/risks.