Skip to content

feat: add lightweight MCP replay context layer#198

Merged
ProfRandom92 merged 3 commits into
mainfrom
feat/mcp-context-layer
May 22, 2026
Merged

feat: add lightweight MCP replay context layer#198
ProfRandom92 merged 3 commits into
mainfrom
feat/mcp-context-layer

Conversation

@ProfRandom92
Copy link
Copy Markdown
Owner

Summary

Adds a lightweight deterministic MCP-compatible replay-aware context layer.

This introduces:

  • compact replay payload generation
  • deterministic replay payload validation
  • token-light prompt context rendering
  • file-backed context save/load helpers
  • deterministic MCP context example artifact
  • exact regeneration validation for the committed artifact

Scope

This is intentionally not:

  • an autonomous agent framework
  • a workflow orchestrator
  • a tool execution runtime
  • a policy engine
  • a vector memory system
  • semantic scoring
  • LLM judging

Validation

  • python -m compileall -q src/comptext_v7/mcp
  • pytest tests/test_mcp_context_layer.py -q

Notes

The MCP context layer is a small replay-aware context integrity foundation. It is designed to preserve operational commitments in compact deterministic form without broadening CompTextv7 beyond fixture-bound replay-integrity validation.

@ProfRandom92 ProfRandom92 merged commit 2c2b504 into main May 22, 2026
7 checks passed
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a lightweight, replay-aware context layer for MCP-style runtime systems, featuring a compression engine for operational commitments, a file-backed context store, and a deterministic admissibility validator. The reviewer feedback focuses on increasing error handling strictness by consistently using RuntimeError for JSON validation and improving robustness during file loading by handling JSONDecodeError alongside FileNotFoundError.

def _object(value: Any, field: str) -> dict[str, Any]:
if isinstance(value, dict):
return value
raise ValueError(f"{field} must be an object")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError when validating JSON data types to maintain strictness.

Suggested change
raise ValueError(f"{field} must be an object")
raise RuntimeError(f"{field} must be an object")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

return []
if isinstance(value, list):
return value
raise ValueError(f"{field} must be a list")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError when validating JSON data types.

Suggested change
raise ValueError(f"{field} must be a list")
raise RuntimeError(f"{field} must be a list")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

value = context.get(key)
if isinstance(value, str) and value.strip():
return value.strip()
raise ValueError("context requires task, task_id, or fixture_id")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used for validation errors during JSON processing.

Suggested change
raise ValueError("context requires task, task_id, or fixture_id")
raise RuntimeError("context requires task, task_id, or fixture_id")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

Comment on lines +38 to +41
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError as exc:
raise KeyError(f"context not found: {task_id}") from exc
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The general rules require handling both FileNotFoundError and JSONDecodeError when loading JSON files, raising a RuntimeError that includes a repo-relative path. Note that using KeyError here might be intended for the API, but the repository rules specify RuntimeError for file loading issues.

Suggested change
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except FileNotFoundError as exc:
raise KeyError(f"context not found: {task_id}") from exc
try:
payload = json.loads(path.read_text(encoding="utf-8"))
except (FileNotFoundError, json.JSONDecodeError) as exc:
raise RuntimeError(f"Failed to load context from {path}: {exc}") from exc
References
  1. When loading JSON files, handle FileNotFoundError and JSONDecodeError by raising a RuntimeError that includes a repo-relative path.

except FileNotFoundError as exc:
raise KeyError(f"context not found: {task_id}") from exc
if not isinstance(payload, dict):
raise ValueError(f"context payload must be an object: {path}")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, use RuntimeError instead of ValueError to validate that the decoded JSON payload is of the expected type.

Suggested change
raise ValueError(f"context payload must be an object: {path}")
raise RuntimeError(f"context payload must be an object: {path}")
References
  1. Validate that the decoded payload is of the expected type using RuntimeError instead of assertions.

edges: list[Edge] = []
for item in value:
if not isinstance(item, list) or len(item) != 2:
raise ValueError(f"{field} entries must be [source, target]")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError for strict validation of JSON data structures.

Suggested change
raise ValueError(f"{field} entries must be [source, target]")
raise RuntimeError(f"{field} entries must be [source, target]")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

raise ValueError(f"{field} entries must be [source, target]")
source, target = item
if not isinstance(source, str) or not source or not isinstance(target, str) or not target:
raise ValueError(f"{field} entries must contain non-empty strings")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError for strict validation of JSON data content.

Suggested change
raise ValueError(f"{field} entries must contain non-empty strings")
raise RuntimeError(f"{field} entries must contain non-empty strings")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

if value is None:
return ()
if not isinstance(value, list):
raise ValueError(f"{field} must be a list")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError for type validation of JSON data.

Suggested change
raise ValueError(f"{field} must be a list")
raise RuntimeError(f"{field} must be a list")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

strings: list[str] = []
for item in value:
if not isinstance(item, str) or not item:
raise ValueError(f"{field} entries must be non-empty strings")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError for strict validation of JSON data content.

Suggested change
raise ValueError(f"{field} entries must be non-empty strings")
raise RuntimeError(f"{field} entries must be non-empty strings")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

def validate_replay_payload(payload: dict[str, Any]) -> dict[str, object]:
"""Return deterministic admissibility evidence for a compact replay payload."""
if not isinstance(payload.get("task"), str) or not payload["task"]:
raise ValueError("payload.task must be a non-empty string")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Per the general rules, RuntimeError should be used instead of ValueError for strict validation of JSON data fields.

Suggested change
raise ValueError("payload.task must be a non-empty string")
raise RuntimeError("payload.task must be a non-empty string")
References
  1. When processing JSON data, raise a RuntimeError for non-expected types to maintain strictness.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant