Skip to content

Commit c604d67

Browse files
authored
feat: add deterministic MCP context CLI
Adds a deterministic CLI/tool entrypoint for the MCP context layer, repo-local Codex workflow skill docs, focused determinism tests, and hardened JSON loading based on review feedback.
1 parent 2c2b504 commit c604d67

7 files changed

Lines changed: 414 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Artifact Validation Skill
2+
3+
## Purpose
4+
5+
Create or update deterministic artifact checks without changing benchmark meaning or unrelated generated outputs.
6+
7+
## When to use
8+
9+
Use for artifacts under `artifacts/`, generator scripts under `scripts/`, regeneration parity tests, stable JSON checks, and committed artifact reproducibility.
10+
11+
## Allowed actions
12+
13+
- Inspect existing artifact and generator patterns first.
14+
- Use deterministic JSON with stable key ordering and stable list ordering.
15+
- Prefer generator-to-committed-artifact parity tests.
16+
- Compare byte-identical text when formatting and key order matter.
17+
- Use fixture-bound inputs and explicit metadata such as `artifact_id`, `version`, `evaluation_mode`, `llm_judges`, and `external_apis` when consistent with nearby artifacts.
18+
19+
## Forbidden actions
20+
21+
- No timestamps, random IDs, environment-dependent fields, or network calls.
22+
- No semantic scoring, embeddings, vector DB, external APIs, or LLM judging.
23+
- No unrelated artifact rewrites.
24+
- No benchmark semantic changes unless explicitly requested.
25+
- No broad refactors.
26+
- No commit or push unless explicitly requested.
27+
28+
## Required validation
29+
30+
- Run the focused artifact test.
31+
- Run the exact generator into a temporary path when testing regeneration.
32+
- Confirm `git diff --stat` only shows intended artifact, script, test, or doc files.
33+
34+
## Stop conditions
35+
36+
- Stop if generated output differs from the committed artifact and the reason is unclear.
37+
- Stop if deterministic metadata cannot be defined without inventing claims.
38+
- Stop if the task would require regenerating unrelated artifacts.
39+
40+
## Preferred prompt pattern
41+
42+
```text
43+
Task: add or tighten one deterministic artifact validation.
44+
Allowed files: one artifact, its generator, focused tests, optional docs.
45+
Validation: generate to tmp, compare to committed artifact, run focused tests.
46+
Done when: output is byte-identical or exact JSON-equivalent as specified.
47+
```
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Docs Positioning Skill
2+
3+
## Purpose
4+
5+
Update CompTextv7 documentation while preserving conservative replay-integrity positioning and scope boundaries.
6+
7+
## When to use
8+
9+
Use for README, docs, reports, positioning, benchmark explanations, MCP docs, artifact narratives, and PR-facing summaries.
10+
11+
## Allowed actions
12+
13+
- Inspect existing docs and nearby wording before editing.
14+
- Keep claims fixture-bound, deterministic, and artifact-backed.
15+
- Prefer small docs-only patches for positioning changes.
16+
- Separate core CompTextv7 documentation from showcase or brand/demo work.
17+
- Use exact validation or artifact names when referencing evidence.
18+
- State limitations and non-goals clearly.
19+
20+
## Forbidden actions
21+
22+
- No production-ready, clinical-grade, universal memory, or solved-memory claims.
23+
- No semantic scoring, embeddings, vector DB, external APIs, or LLM judging claims.
24+
- No autonomous agent framework, workflow orchestrator, runtime tool execution, or policy-engine positioning.
25+
- No invented benchmark results or unrun validation claims.
26+
- No Chilli/Hatch/Pet or showcase asset changes unless explicitly requested.
27+
- No commit or push unless explicitly requested.
28+
29+
## Required validation
30+
31+
- Run `git diff --stat`.
32+
- Confirm only intended docs files changed.
33+
- Run no broad checks unless the docs change is tied to generated artifacts or explicitly requested.
34+
35+
## Stop conditions
36+
37+
- Stop if documentation would broaden project identity beyond deterministic replay-integrity validation.
38+
- Stop if a metric lacks a committed artifact or exact source.
39+
- Stop if requested copy conflicts with repository non-goals.
40+
41+
## Preferred prompt pattern
42+
43+
```text
44+
Task: make one docs-only positioning update.
45+
Allowed files: exact docs paths.
46+
Constraints: fixture-bound claims, no overclaims, no showcase/core mixing.
47+
Validation: git diff --stat and changed-file scope check.
48+
Done when: wording matches existing CompTextv7 positioning and non-goals.
49+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Git PR Workflow Skill
2+
3+
## Purpose
4+
5+
Handle CompTextv7 branch sync, local commits, pushes, and pull-request setup with staged-file discipline.
6+
7+
## When to use
8+
9+
Use for branch setup, syncing from `main`, local commits, staged-file review, pushing current branches, PR creation, and CI follow-up.
10+
11+
## Allowed actions
12+
13+
- Inspect `git status --short` before git operations.
14+
- Use non-destructive git commands.
15+
- Stage only explicitly allowed paths.
16+
- Show staged files with `git diff --cached --name-only` before committing.
17+
- Commit only after staged files match the requested scope.
18+
- Push only when explicitly requested and the working tree is clean.
19+
- Create a PR only when explicitly requested and GitHub CLI is available and authenticated.
20+
21+
## Forbidden actions
22+
23+
- No force push.
24+
- No merge unless explicitly requested.
25+
- No branch deletion.
26+
- No destructive commands such as `git reset --hard` or `git clean -fd`.
27+
- No changing commits unless explicitly requested.
28+
- No commit or push unless explicitly requested.
29+
30+
## Required validation
31+
32+
- Before commit: `git status --short`, `git diff --stat`, and `git diff --cached --name-only`.
33+
- Before push: clean `git status --short` and current branch confirmation.
34+
- For PRs: include validation commands actually run; do not claim CI status unless checked.
35+
36+
## Stop conditions
37+
38+
- Stop if working tree contains unrelated changes.
39+
- Stop if staged files exceed the allowed scope.
40+
- Stop if GitHub CLI is missing or unauthenticated; print manual commands.
41+
- Stop if sync or push fails.
42+
43+
## Preferred prompt pattern
44+
45+
```text
46+
Task: create a local commit or PR for current scoped changes.
47+
Allowed files: exact paths.
48+
Validation already run: exact commands.
49+
Steps: status, stage exact files, show staged list, commit/push only if scoped.
50+
Done when: local commit or PR is created, no merge performed.
51+
```
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# MCP Context Layer Skill
2+
3+
## Purpose
4+
5+
Work on the lightweight MCP-compatible replay-aware context layer while preserving CompTextv7 replay-integrity boundaries.
6+
7+
## When to use
8+
9+
Use for changes involving `src/comptext_v7/mcp/`, `scripts/mcp_context_cli.py`, compact replay payloads, prompt-context rendering, validation, save/load helpers, or token-light replay-safe context.
10+
11+
## Allowed actions
12+
13+
- Inspect repository files before editing.
14+
- Make the smallest safe patch.
15+
- Reuse `build_replay_payload`, `render_prompt_context`, `validate_replay_payload`, `ContextStore`, `save_context`, and `load_context`.
16+
- Keep examples fixture-bound, usually `fixtures/mcp_trace_replay_v1/original`.
17+
- Add focused tests in `tests/test_mcp_context_layer.py`.
18+
- Update `docs/mcp_context_layer.md` when public behavior changes.
19+
20+
## Forbidden actions
21+
22+
- No semantic scoring, embeddings, vector DB, external APIs, or LLM judging.
23+
- No autonomous orchestration or runtime tool execution unless explicitly scoped.
24+
- No broad refactors or changes to existing benchmark semantics.
25+
- No ContractValidator behavior changes unless explicitly requested.
26+
- No commit or push unless explicitly requested.
27+
28+
## Required validation
29+
30+
- `python -m compileall -q src/comptext_v7/mcp`
31+
- `pytest tests/test_mcp_context_layer.py -q`
32+
- If CLI behavior changes, run the affected `python scripts/mcp_context_cli.py ...` command.
33+
34+
## Stop conditions
35+
36+
- Stop if fixture shape is unclear.
37+
- Stop if a change requires non-deterministic behavior.
38+
- Stop if requested behavior would broaden MCP context into orchestration, policy enforcement, or tool execution.
39+
40+
## Preferred prompt pattern
41+
42+
```text
43+
Task: make one deterministic MCP context-layer change.
44+
Allowed files: exact MCP files, focused tests, and docs.
45+
Validation: compile MCP package and run tests/test_mcp_context_layer.py.
46+
Done when: replay-safe ordering, validation output, and no raw trace leakage are verified.
47+
```

docs/mcp_context_layer.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ example generated from `fixtures/mcp_trace_replay_v1/original`. It contains the
8686
compact replay payload, prompt-context rendering, and validation result for the
8787
baseline MCP trace replay fixture.
8888

89+
## CLI usage
90+
91+
Generate compact prompt context with validation from the baseline MCP fixture:
92+
93+
```bash
94+
python scripts/mcp_context_cli.py \
95+
--fixture fixtures/mcp_trace_replay_v1/original \
96+
--render-prompt \
97+
--validate
98+
```
99+
100+
Add `--json` to emit deterministic JSON containing the compact replay payload,
101+
optional prompt context, and optional validation result.
102+
89103
## Relationship to MCP
90104

91105
This layer augments context integrity for MCP-compatible systems. It is not an

scripts/mcp_context_cli.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""Deterministic CLI for compact MCP context-layer outputs."""
2+
3+
from __future__ import annotations
4+
5+
import argparse
6+
import json
7+
import sys
8+
from pathlib import Path
9+
from typing import Any
10+
11+
REPO_ROOT = Path(__file__).resolve().parents[1]
12+
if str(REPO_ROOT) not in sys.path:
13+
sys.path.insert(0, str(REPO_ROOT))
14+
15+
from src.comptext_v7.mcp import build_replay_payload, render_prompt_context, validate_replay_payload
16+
17+
DEFAULT_FIXTURE = Path("fixtures/mcp_trace_replay_v1/original")
18+
19+
20+
def _repo_relative(path: Path) -> str:
21+
resolved = path.resolve()
22+
try:
23+
return resolved.relative_to(REPO_ROOT).as_posix()
24+
except ValueError:
25+
return path.as_posix()
26+
27+
28+
def _load_json(path: Path) -> dict[str, Any]:
29+
display_path = _repo_relative(path)
30+
try:
31+
payload = json.loads(path.read_text(encoding="utf-8"))
32+
except FileNotFoundError as exc:
33+
raise RuntimeError(f"missing required fixture file: {display_path}") from exc
34+
except json.JSONDecodeError as exc:
35+
raise RuntimeError(f"invalid JSON in fixture file: {display_path}: {exc}") from exc
36+
if not isinstance(payload, dict):
37+
raise RuntimeError(f"fixture file must contain a JSON object: {display_path}")
38+
return payload
39+
40+
41+
def load_fixture_context(fixture: Path) -> dict[str, Any]:
42+
fixture_path = fixture if fixture.is_absolute() else REPO_ROOT / fixture
43+
return {
44+
"task": fixture_path.parent.name,
45+
"trace": _load_json(fixture_path / "trace.json"),
46+
"state": _load_json(fixture_path / "state.json"),
47+
"dependency_graph": _load_json(fixture_path / "dependency_graph.json"),
48+
}
49+
50+
51+
def build_cli_output(fixture: Path, *, include_prompt: bool, include_validation: bool) -> dict[str, Any]:
52+
fixture_path = fixture if fixture.is_absolute() else REPO_ROOT / fixture
53+
replay_payload = build_replay_payload(load_fixture_context(fixture_path))
54+
output: dict[str, Any] = {
55+
"replay_payload": replay_payload,
56+
"source_fixture_path": _repo_relative(fixture_path),
57+
}
58+
59+
validation = validate_replay_payload(replay_payload) if include_validation else None
60+
if validation is not None:
61+
output["validation"] = validation
62+
if include_prompt:
63+
prompt_payload = {**replay_payload}
64+
if validation is not None:
65+
prompt_payload["validation"] = validation
66+
output["prompt_context"] = render_prompt_context(prompt_payload)
67+
68+
return output
69+
70+
71+
def _parse_args(argv: list[str]) -> argparse.Namespace:
72+
parser = argparse.ArgumentParser(description="Generate deterministic MCP context-layer output from a fixture.")
73+
parser.add_argument("--fixture", type=Path, default=DEFAULT_FIXTURE, help="Fixture directory containing trace/state/dependency_graph JSON files.")
74+
parser.add_argument("--json", action="store_true", help="Emit deterministic JSON output.")
75+
parser.add_argument("--render-prompt", action="store_true", help="Include or emit compact prompt context.")
76+
parser.add_argument("--validate", action="store_true", help="Include validation/admissibility result.")
77+
return parser.parse_args(argv)
78+
79+
80+
def main(argv: list[str] | None = None) -> int:
81+
args = _parse_args(sys.argv[1:] if argv is None else argv)
82+
output = build_cli_output(args.fixture, include_prompt=args.render_prompt, include_validation=args.validate)
83+
84+
if args.json or not args.render_prompt:
85+
sys.stdout.write(json.dumps(output, indent=2, sort_keys=True) + "\n")
86+
return 0
87+
88+
prompt_context = output.get("prompt_context")
89+
if not isinstance(prompt_context, str):
90+
prompt_context = render_prompt_context(output["replay_payload"])
91+
sys.stdout.write(prompt_context + "\n")
92+
return 0
93+
94+
95+
if __name__ == "__main__":
96+
raise SystemExit(main())

0 commit comments

Comments
 (0)