-
Notifications
You must be signed in to change notification settings - Fork 9.5k
feat(integration): add status reporting #2674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PascalThuet
wants to merge
21
commits into
github:main
Choose a base branch
from
PascalThuet:codex/integration-doctor
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
0e55a1e
feat(integration): add doctor diagnostics
PascalThuet 9a70826
fix(integration): address doctor review feedback
PascalThuet 5d5587a
fix(integration): harden doctor diagnostics
PascalThuet d420ce6
fix(integration): rename doctor diagnostics to status
PascalThuet 81ed372
fix(integration): address status review feedback
PascalThuet 485b143
fix(integration): validate status manifest keys
PascalThuet b3a245b
fix(integration): escape status report output
PascalThuet 7d505aa
fix(integration): address status review feedback
PascalThuet 40477a2
fix(integration): harden status manifest checks
PascalThuet ee8ddc0
fix(integration): tighten status diagnostics
PascalThuet 99db1bb
fix(integration): clarify status state sources
PascalThuet 9dedcf4
fix(integration): report unknown multi-install safety
PascalThuet 1596fe2
fix(integration): mark empty safety as unknown
PascalThuet 934b152
fix(integration): ignore invalid raw installed list
PascalThuet c8d9faa
fix(integration): report actual manifest checks
PascalThuet 646ab9d
fix(integration): tighten status contract invariants
PascalThuet 982e8a6
fix(integration): harden manifest status paths
PascalThuet 4b40ef2
fix(integration): avoid symlink target stat
PascalThuet 6d5383e
fix(integration): clarify status edge cases
PascalThuet 67d28d4
test(integration): pin status filesystem guards
PascalThuet 928d89d
fix(integration): tighten status path checks
PascalThuet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,271 @@ | ||
| """Read-only diagnostics for project integration state.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| from .integration_state import ( | ||
| INTEGRATION_JSON, | ||
| IntegrationReadError, | ||
| default_integration_key, | ||
| installed_integration_keys, | ||
| try_read_integration_json, | ||
| ) | ||
| from .integrations import INTEGRATION_REGISTRY | ||
| from .integrations.manifest import IntegrationManifest | ||
|
|
||
| _MANIFEST_READ_ERRORS = (ValueError, FileNotFoundError, OSError, UnicodeDecodeError) | ||
|
|
||
|
|
||
| def _finding( | ||
| severity: str, | ||
| code: str, | ||
| message: str, | ||
| *, | ||
| integration: str | None = None, | ||
| path: str | None = None, | ||
| suggestion: str | None = None, | ||
| ) -> dict[str, str]: | ||
| item = { | ||
| "severity": severity, | ||
| "code": code, | ||
| "message": message, | ||
| } | ||
| if integration: | ||
| item["integration"] = integration | ||
| if path: | ||
| item["path"] = path | ||
| if suggestion: | ||
| item["suggestion"] = suggestion | ||
| return item | ||
|
|
||
|
|
||
| def _status(findings: list[dict[str, str]]) -> str: | ||
| if any(item["severity"] == "error" for item in findings): | ||
| return "error" | ||
| if findings: | ||
| return "warning" | ||
| return "ok" | ||
|
|
||
|
|
||
| def _integration_state_error_message(error: IntegrationReadError) -> str: | ||
| if error.kind == "decode": | ||
| return f"{INTEGRATION_JSON} contains invalid JSON or is not valid UTF-8." | ||
| if error.kind == "os": | ||
| return f"Could not read {INTEGRATION_JSON}." | ||
| if error.kind == "not_object": | ||
| return f"{INTEGRATION_JSON} must contain a JSON object, got {error.detail}." | ||
| if error.kind == "schema_too_new": | ||
| return ( | ||
| f"{INTEGRATION_JSON} uses integration state schema {error.schema}, " | ||
| "which is newer than this CLI supports." | ||
| ) | ||
| return f"Could not inspect {INTEGRATION_JSON}." | ||
|
|
||
|
|
||
| def _safe_manifest_file(project_root: Path, rel: str) -> Path | None: | ||
| rel_path = Path(rel) | ||
| if rel_path.is_absolute() or ".." in rel_path.parts: | ||
| return None | ||
| return project_root / rel_path | ||
|
|
||
|
|
||
| def _manifest_missing_files(manifest: IntegrationManifest) -> list[str]: | ||
| missing: list[str] = [] | ||
| for rel in manifest.files: | ||
| path = _safe_manifest_file(manifest.project_root, rel) | ||
| if path is None: | ||
| continue | ||
| if not path.exists() and not path.is_symlink(): | ||
|
PascalThuet marked this conversation as resolved.
Outdated
|
||
| missing.append(rel) | ||
| return missing | ||
|
|
||
|
|
||
| def diagnose_integration_project(project_root: Path) -> dict[str, Any]: | ||
| """Return a machine-readable integration health report for *project_root*.""" | ||
| findings: list[dict[str, str]] = [] | ||
| state, error = try_read_integration_json(project_root) | ||
| if error is not None: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "integration-state-unreadable", | ||
| _integration_state_error_message(error), | ||
| path=INTEGRATION_JSON, | ||
| suggestion=f"Fix or delete {INTEGRATION_JSON}, then retry.", | ||
| ) | ||
| ) | ||
| return _build_report(None, [], findings, {}, True) | ||
|
|
||
| if state is None: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "integration-state-missing", | ||
| f"{INTEGRATION_JSON} is missing.", | ||
| path=INTEGRATION_JSON, | ||
| suggestion="Run `specify integration install <key>` to install an integration.", | ||
| ) | ||
| ) | ||
| return _build_report(None, [], findings, {}, True) | ||
|
|
||
| default_key = default_integration_key(state) | ||
| installed_keys = installed_integration_keys(state) | ||
| if not installed_keys: | ||
| findings.append( | ||
| _finding( | ||
| "warning", | ||
| "no-installed-integrations", | ||
| "No installed integrations are recorded.", | ||
| suggestion="Run `specify integration install <key>` to install one.", | ||
| ) | ||
| ) | ||
| return _build_report(default_key, installed_keys, findings, {}, True) | ||
|
|
||
| if default_key is None: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "default-integration-missing", | ||
| "No default integration is recorded.", | ||
| suggestion="Run `specify integration use <key>` after choosing an installed integration.", | ||
| ) | ||
| ) | ||
|
|
||
| known_installed = [key for key in installed_keys if key in INTEGRATION_REGISTRY] | ||
| for key in installed_keys: | ||
| if key not in INTEGRATION_REGISTRY: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "unknown-integration", | ||
| f"Integration '{key}' is installed but is not known to this CLI.", | ||
| integration=key, | ||
| suggestion="Upgrade Spec Kit, or uninstall the stale integration metadata.", | ||
| ) | ||
| ) | ||
|
|
||
| unsafe = [ | ||
| key for key in known_installed | ||
| if not getattr(INTEGRATION_REGISTRY[key], "multi_install_safe", False) | ||
| ] | ||
| if len(installed_keys) > 1 and unsafe: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "unsafe-multi-install", | ||
| ( | ||
| "Installed integrations are not all declared multi-install safe: " | ||
| + ", ".join(sorted(unsafe)) | ||
| ), | ||
| suggestion="Use `specify integration use <key>` to change defaults, or `switch` only when replacing integrations.", | ||
| ) | ||
| ) | ||
|
|
||
| manifest_files_by_path: dict[str, list[str]] = {} | ||
| manifest_summaries: dict[str, dict[str, Any]] = {} | ||
| for key in installed_keys: | ||
| manifest_path = project_root / ".specify" / "integrations" / f"{key}.manifest.json" | ||
| if not manifest_path.exists(): | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "manifest-missing", | ||
| f"Manifest for integration '{key}' is missing.", | ||
| integration=key, | ||
| path=manifest_path.relative_to(project_root).as_posix(), | ||
| suggestion=f"Run `specify integration upgrade {key}` or reinstall the integration.", | ||
| ) | ||
| ) | ||
| manifest_summaries[key] = { | ||
| "manifest": manifest_path.relative_to(project_root).as_posix(), | ||
| "tracked_files": 0, | ||
| "missing_files": [], | ||
| "modified_files": [], | ||
| } | ||
| continue | ||
|
|
||
| try: | ||
| manifest = IntegrationManifest.load(key, project_root) | ||
| except _MANIFEST_READ_ERRORS as exc: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "manifest-unreadable", | ||
| f"Manifest for integration '{key}' is unreadable: {exc}", | ||
| integration=key, | ||
| path=manifest_path.relative_to(project_root).as_posix(), | ||
| suggestion=f"Fix the manifest or reinstall integration '{key}'.", | ||
| ) | ||
| ) | ||
| continue | ||
|
|
||
| missing = _manifest_missing_files(manifest) | ||
| modified = manifest.check_modified() | ||
| manifest_summaries[key] = { | ||
| "manifest": manifest_path.relative_to(project_root).as_posix(), | ||
| "tracked_files": len(manifest.files), | ||
| "missing_files": missing, | ||
| "modified_files": modified, | ||
| } | ||
|
|
||
| for rel in manifest.files: | ||
| manifest_files_by_path.setdefault(rel, []).append(key) | ||
| if missing: | ||
| findings.append( | ||
| _finding( | ||
| "error", | ||
| "managed-files-missing", | ||
| f"{len(missing)} managed file(s) are missing for integration '{key}'.", | ||
| integration=key, | ||
| suggestion=f"Run `specify integration upgrade {key}` to regenerate managed files.", | ||
| ) | ||
| ) | ||
| if modified: | ||
| findings.append( | ||
| _finding( | ||
| "warning", | ||
| "managed-files-modified", | ||
| f"{len(modified)} managed file(s) were modified for integration '{key}'.", | ||
| integration=key, | ||
| suggestion="Review the changes before running `specify integration upgrade --force`.", | ||
| ) | ||
| ) | ||
|
|
||
| for rel, keys in sorted(manifest_files_by_path.items()): | ||
| if len(keys) > 1: | ||
| findings.append( | ||
| _finding( | ||
| "warning", | ||
| "managed-file-collision", | ||
| f"Managed file '{rel}' is tracked by multiple integrations: {', '.join(sorted(keys))}.", | ||
| path=rel, | ||
| suggestion="Review the manifests before uninstalling or upgrading these integrations.", | ||
| ) | ||
| ) | ||
|
|
||
| multi_install_safe = not (len(installed_keys) > 1 and unsafe) | ||
| return _build_report(default_key, installed_keys, findings, manifest_summaries, multi_install_safe) | ||
|
|
||
|
|
||
| def _build_report( | ||
| default_key: str | None, | ||
| installed_keys: list[str], | ||
| findings: list[dict[str, str]], | ||
| manifests: dict[str, dict[str, Any]], | ||
| multi_install_safe: bool, | ||
| ) -> dict[str, Any]: | ||
| missing_count = sum(len(item.get("missing_files", [])) for item in manifests.values()) | ||
| modified_count = sum(len(item.get("modified_files", [])) for item in manifests.values()) | ||
| return { | ||
| "status": _status(findings), | ||
| "default_integration": default_key, | ||
| "installed_integrations": installed_keys, | ||
| "multi_install_safe": multi_install_safe, | ||
| "shared_templates_aligned_to": default_key, | ||
| "missing_managed_files": missing_count, | ||
| "modified_managed_files": modified_count, | ||
| "manifests": manifests, | ||
| "findings": findings, | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.