Skip to content

feat(doctor): add operational diagnostics and repair#287

Merged
Alan-TheGentleman merged 2 commits intomainfrom
feat/doctor-diagnostic
Apr 30, 2026
Merged

feat(doctor): add operational diagnostics and repair#287
Alan-TheGentleman merged 2 commits intomainfrom
feat/doctor-diagnostic

Conversation

@Alan-TheGentleman
Copy link
Copy Markdown
Collaborator

🔗 Linked Issue

Closes #286


🏷️ PR Type

  • type:bug — Bug fix
  • type:feature — New feature
  • type:docs — Documentation only
  • type:refactor — Code refactoring (no behavior change)
  • type:chore — Maintenance, dependencies, tooling
  • type:breaking-change — Breaking change

📝 Summary

  • Adds engram doctor read-only operational diagnostics with CLI JSON/text output and MCP mem_doctor parity.
  • Adds explicit engram doctor repair plan/dry-run/apply flows for session-scoped project reclassification with backup-before-apply safety.
  • Documents diagnostic checks, repair boundaries, and cloned-DB manual verification for the sias-app contamination shape.

📂 Changes

File Change
cmd/engram/doctor.go Adds doctor CLI plus repair mode validation/output.
internal/diagnostic/ Adds diagnostic envelope, registry, checks, and repair planner.
internal/store/diagnostic.go Adds diagnostic read helpers plus backup and session-scoped reclassification.
internal/mcp/mcp.go Registers mem_doctor as a read-only agent tool.
docs/DOCTOR.md Documents check catalog, MCP usage, and repair workflow.
openspec/changes/doctor-* Captures SDD artifacts and verification evidence.

🧪 Test Plan

  • Unit tests pass locally: go test ./...
  • E2E tests pass locally: go test -tags e2e ./internal/server/...
  • Manually tested the affected functionality

Manual testing:

  • Ran diagnostic and repair flows against SQLite .backup clones only; production ~/.engram/engram.db was not mutated.
  • Verified --plan and --dry-run are non-mutating.
  • Verified --apply creates a backup and updates only sessions.project, observations.project, and user_prompts.project.
  • Verified final engram doctor --json --project sias-app returned OK in the repaired clone.

🤖 Automated Checks

Check What it verifies Status
Check Issue Reference PR body contains Closes #N / Fixes #N / Resolves #N
Check Issue Has status:approved Linked issue has status:approved label
Check PR Has type:* Label PR has exactly one type:* label
Unit Tests go test ./... passes
E2E Tests go test -tags e2e ./internal/server/... passes

✅ Contributor Checklist

  • I linked an approved issue above (Closes #N)
  • I added exactly one type:* label to this PR
  • I ran unit tests locally: go test ./...
  • I ran e2e tests locally: go test -tags e2e ./internal/server/...
  • Docs updated (if behavior changed)
  • Commits follow conventional commits format
  • No Co-Authored-By trailers in commits

💬 Notes for Reviewers

This is a stacked PR based on feat/memory-conflict-surfacing-cloud-sync so the review diff stays scoped to the doctor work instead of reintroducing the prior memory-conflict/cloud-sync branch history.

Copilot AI review requested due to automatic review settings April 29, 2026 22:13
@Alan-TheGentleman Alan-TheGentleman added the type:feature New feature label Apr 29, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a new operational “doctor” surface to Engram for read-only diagnostics (CLI + MCP parity), plus a narrowly-scoped, backup-gated doctor repair flow to reclassify project-contaminated session data.

Changes:

  • Introduces internal/diagnostic runner/registry + MVP checks and wires them into engram doctor and MCP mem_doctor.
  • Adds engram doctor repair plan/dry-run/apply workflow with a diagnostic-based planner and store-owned backup + session-scoped project reclassification.
  • Adds documentation and OpenSpec artifacts (design/spec/tasks/verification/manual verification) for diagnostics and repair.

Reviewed changes

Copilot reviewed 33 out of 33 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
cmd/engram/main.go Wires doctor into CLI dispatch and adds a shared runDiagnostics adapter.
cmd/engram/doctor.go Implements engram doctor (text/JSON) and engram doctor repair (plan/dry-run/apply) CLI flows.
cmd/engram/doctor_test.go Adds CLI tests for diagnostics, MCP parity, and repair validation/JSON/apply behavior.
internal/diagnostic/diagnostic.go Adds core diagnostic types, report envelope, runner, and error report helper.
internal/diagnostic/registry.go Adds deterministic check registry + invalid-check error handling.
internal/diagnostic/checks.go Implements MVP checks (directory mismatch, manual session mismatch, mutation required fields, SQLite lock contention).
internal/diagnostic/diagnostic_test.go Adds unit tests for runner/registry and check branches (incl. deterministic lock contention seam).
internal/diagnostic/repair.go Adds repair planning for supported mismatch checks into deterministic action lists.
internal/diagnostic/repair_test.go Adds unit tests for repair planning rules (trusted evidence, manual naming constraints, skip reasons).
internal/store/diagnostic.go Adds store read helpers for diagnostics plus backup + apply/estimate APIs for session-scoped reclassification.
internal/store/diagnostic_repair_test.go Adds store tests for estimate non-mutation and apply safety boundaries (backup + allowed tables only).
internal/store/store_test.go Adds tests for new diagnostic read helpers and lock snapshot non-mutation.
internal/mcp/mcp.go Registers mem_doctor tool (agent profile) and handler using the shared diagnostic runner.
internal/mcp/mcp_test.go Updates tool allowlists/count expectations and adds mem_doctor contract tests (auto-detect, unknown project, envelope shape).
docs/DOCTOR.md Documents doctor/repair CLI forms, MCP contract, check catalog, safety boundaries, and clone-safe workflow.
openspec/changes/doctor-diagnostic/spec.md Points root spec note to canonical spec deltas to avoid conflicting requirements.
openspec/changes/doctor-diagnostic/proposal.md Captures intent/scope/approach/risks for the diagnostic feature.
openspec/changes/doctor-diagnostic/design.md Documents architecture decisions and contracts for diagnostic runner/CLI/MCP.
openspec/changes/doctor-diagnostic/exploration.md Records exploration context and rationale for the approach.
openspec/changes/doctor-diagnostic/tasks.md Tracks implementation tasks/phases for diagnostics.
openspec/changes/doctor-diagnostic/manual-verification.md Adds manual verification matrix + clone walkthrough evidence.
openspec/changes/doctor-diagnostic/apply-progress.md Records apply progress and verification gap fixes for diagnostics.
openspec/changes/doctor-diagnostic/verify-report.md Adds verification report for diagnostics (tests, docs alignment, evidence mapping).
openspec/changes/doctor-diagnostic/specs/operational-diagnostics/spec.md Defines canonical diagnostic requirements and scenarios.
openspec/changes/doctor-diagnostic/specs/mcp-project-resolution/spec.md Extends MCP read-tool optional project requirement to include mem_doctor.
openspec/changes/doctor-repair-reclassify-project/proposal.md Captures intent/scope/approach/risks for repair MVP.
openspec/changes/doctor-repair-reclassify-project/design.md Documents repair architecture, interfaces, and testing strategy.
openspec/changes/doctor-repair-reclassify-project/exploration.md Records exploration context and repair design implications.
openspec/changes/doctor-repair-reclassify-project/tasks.md Tracks implementation tasks/phases for repair.
openspec/changes/doctor-repair-reclassify-project/manual-verification.md Documents clone-safe manual verification commands and observed outputs.
openspec/changes/doctor-repair-reclassify-project/apply-progress.md Records apply progress and verification notes for repair MVP.
openspec/changes/doctor-repair-reclassify-project/verify-report.md Adds verification report for repair MVP (requirements mapping + evidence).
openspec/changes/doctor-repair-reclassify-project/specs/operational-diagnostics/spec.md Defines repair-related requirement deltas and scenarios.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/engram/doctor.go
Comment on lines +62 to +66
if err != nil {
report = diagnostic.ErrorReport(project, err)
if jsonOut {
writeDoctorJSON(report)
} else {
Comment on lines +100 to +104
}
nameProject := normalizeProjectName(strings.TrimPrefix(session.Name, "manual-save-"))
sessionProject := normalizeProjectName(session.Project)
if nameProject == "" || sessionProject == "" || nameProject == sessionProject || !knownProjects[nameProject] {
continue
Comment on lines +83 to +86
### Requirement: REQ-OD-005 MVP Non-Repair Constraint

The doctor MVP MUST NOT perform repair, apply, or write transactions. `requires_confirmation` MUST be false for all MVP checks because no apply path exists.

@Alan-TheGentleman Alan-TheGentleman changed the base branch from feat/memory-conflict-surfacing-cloud-sync to main April 30, 2026 09:15
@Alan-TheGentleman Alan-TheGentleman merged commit cacd08d into main Apr 30, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature New feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(doctor): detect and repair project-contaminated sessions

2 participants