Files
MyAiConfiguration/scripts/commands/doctor.ps1
T
Julian LechnerandClaude Opus 5.5 82d9992b3a fix(qmd): restart daemon on update, check version, add doctor checks
- Stop the running QMD daemon before install and update replace the
  runtime scripts, so the next search loads the new code and models and
  Windows no longer keeps old model files locked.
- Read the installed @tobilu/qmd version: reinstall a setup-owned package
  at the tested version, and warn about a foreign one of another version.
- Add QMD checks to doctor (PowerShell and Bash) through `qmd.py doctor`:
  runtime script digests, package version, model files, and daemon health.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 09:58:48 +01:00

143 lines
10 KiB
PowerShell

[CmdletBinding()]
param([switch]$Summary)
$ErrorActionPreference = 'Stop'
$root = Split-Path (Split-Path $PSScriptRoot -Parent) -Parent
. (Join-Path $root 'scripts/lib/manifest.ps1')
. (Join-Path $root 'shared/hooks/scripts/Resolve-Python.ps1')
$homePath = [Environment]::GetFolderPath('UserProfile')
if ([string]::IsNullOrWhiteSpace($homePath)) { $homePath = $HOME }
$script:fail = $false
$script:checkCount = 0
function Result($state, $message) {
$script:checkCount++
if ($state -eq 'FAIL') { $script:fail = $true }
if (-not ($Summary -and $state -eq 'PASS')) {
Write-Output ("$state $message")
}
}
function Invoke-DoctorCommand {
param([string]$Command, [string[]]$Arguments)
# PowerShell 5.1 turns native stderr into error records, even when the process succeeds.
$ErrorActionPreference = 'Continue'
$PSNativeCommandUseErrorActionPreference = $false
try {
$executable = Get-Command $Command -ErrorAction Stop
$output = @(& $executable @Arguments 2>&1)
return [pscustomobject]@{ Output = $output; ExitCode = $LASTEXITCODE }
} catch {
return [pscustomobject]@{ Output = @($_); ExitCode = 1 }
}
}
foreach ($tool in @('codex','claude')) {
if (Get-Command $tool -ErrorAction SilentlyContinue) {
$probe = Invoke-DoctorCommand -Command $tool -Arguments @('--version')
if ($probe.ExitCode -eq 0) { Result 'PASS' "$tool available ($($probe.Output -join ' '))" }
else { Result 'FAIL' "$tool --version failed (exit $($probe.ExitCode)): $($probe.Output -join ' ')" }
} else { Result 'WARN' "$tool unavailable" }
}
if (Get-Command jq -ErrorAction SilentlyContinue) { Result 'PASS' 'jq available for Claude Bash status line' } else { Result 'WARN' 'jq unavailable; Claude Bash status line is disabled' }
# Globally installed npm CLIs (qmd, mcporter) must be callable by agents, so npm's global bin directory belongs on PATH.
if (Get-Command npm -ErrorAction SilentlyContinue) {
$npmPrefix = @((Invoke-DoctorCommand -Command 'npm' -Arguments @('prefix', '--global')).Output | ForEach-Object { "$_".Trim() } | Where-Object { $_ }) | Select-Object -Last 1
if ($npmPrefix) {
$npmBin = if ([Environment]::OSVersion.Platform -eq [PlatformID]::Win32NT) { $npmPrefix } else { Join-Path $npmPrefix 'bin' }
$pathEntries = @($env:PATH -split [regex]::Escape([string][System.IO.Path]::PathSeparator) | ForEach-Object { $_.TrimEnd([char[]]@('\', '/')) })
if ($pathEntries -contains $npmBin.TrimEnd([char[]]@('\', '/'))) { Result 'PASS' "npm global bin directory is on PATH ($npmBin)" }
else { Result 'WARN' "npm global bin directory $npmBin is not on PATH; agents cannot call globally installed CLIs such as mcporter. Add it to your user PATH and restart the terminal." }
}
}
$python = Get-PythonCommand
if ($python) { Result 'PASS' "Python available ($($python.Display))" }
else { Result 'FAIL' "$(Get-PythonMissingMessage) Without it, build validation fails and the completion-evidence, correction-prompt, and session-state hooks are silently skipped." }
# QMD local search: reports nothing when the setup did not enable it.
if ($python) {
$qmdChecks = Invoke-DoctorCommand -Command $python.Path -Arguments (@($python.Arguments) + @((Join-Path $root 'scripts/lib/qmd.py'), 'doctor', '--root', $root, '--home', $homePath))
if ($qmdChecks.ExitCode -ne 0) { Result 'WARN' "QMD checks failed: $($qmdChecks.Output -join ' ')" }
else { foreach ($line in $qmdChecks.Output) { if ("$line" -match '^(PASS|WARN|FAIL) (.+)$') { Result $Matches[1] $Matches[2] } } }
}
$sourceAgentCount = @(Get-ChildItem (Join-Path $root 'shared/agents') -Directory -ErrorAction SilentlyContinue).Count
$sourceSkillCount = @(Get-ChildItem (Join-Path $root 'shared/skills') -Filter 'SKILL.md' -Recurse -ErrorAction SilentlyContinue).Count
$rulesRoot = Join-Path $root 'shared/rules'
$generalRulePath = Join-Path $rulesRoot 'general.md'
$ruleSkillCount = @(Get-ChildItem $rulesRoot -File -Filter '*.md' -Recurse | Where-Object { $_.FullName -ne $generalRulePath }).Count
foreach ($shell in @('powershell','bash')) {
if (Test-Path (Join-Path $root "generated/codex-$shell/AGENTS.md")) { Result 'PASS' "generated output present ($shell)" } else { Result 'FAIL' "generated output missing ($shell); run build" }
if ((Test-Path (Join-Path $root "generated/codex-$shell/hooks/scripts/Validate-CommandSafety.ps1")) -and (Test-Path (Join-Path $root "generated/codex-$shell/hooks/scripts/validate-command-safety.sh")) -and (Test-Path (Join-Path $root "generated/claude-$shell/hooks/scripts/Validate-CommandSafety.ps1")) -and (Test-Path (Join-Path $root "generated/claude-$shell/hooks/scripts/validate-command-safety.sh"))) { Result 'PASS' "generated hooks present ($shell)" } else { Result 'FAIL' "generated hooks missing ($shell); run build" }
foreach ($client in @('codex','claude')) {
$package = Join-Path $root "generated/$client-$shell"
if (-not (Test-Path $package)) { continue }
$agentCount = @(Get-ChildItem (Join-Path $package 'agents') -File -ErrorAction SilentlyContinue).Count
if ($agentCount -eq $sourceAgentCount) { Result 'PASS' "$client-$shell agent count matches source ($agentCount)" } else { Result 'FAIL' "$client-$shell agent count $agentCount does not match source ($sourceAgentCount)" }
$expectedSkillCount = if ($client -eq 'claude') { $sourceSkillCount + $ruleSkillCount } else { $sourceSkillCount }
$skillCount = @(Get-ChildItem (Join-Path $package 'skills') -Filter 'SKILL.md' -Recurse -ErrorAction SilentlyContinue).Count
if ($skillCount -eq $expectedSkillCount) { Result 'PASS' "$client-$shell skill count matches source ($skillCount)" } else { Result 'FAIL' "$client-$shell skill count $skillCount does not match source ($expectedSkillCount)" }
$placeholderHits = @(Get-ChildItem $package -File -Recurse | Where-Object { ((Get-Content -LiteralPath $_.FullName -Raw) -replace '__AI_CONFIG_ROOT__|__POWERSHELL_COMMAND__|__CLAUDE_CODE_MAX_TOOL_USE_CONCURRENCY__', '') -cmatch '__[A-Z0-9_]*[A-Z0-9][A-Z0-9_]*__' })
if ($placeholderHits.Count -eq 0) { Result 'PASS' "$client-$shell has no unresolved placeholders" } else { Result 'FAIL' "$client-$shell has unresolved placeholders: $($placeholderHits.FullName -join ', ')" }
}
}
foreach ($path in @('.codex/AGENTS.md','.codex/config.toml','.claude/CLAUDE.md','.claude/settings.json')) {
if (Test-Path (Join-Path $homePath $path)) { Result 'PASS' "installed $path" } else { Result 'WARN' "not installed $path" }
}
$settingsPath = Join-Path $homePath '.claude/settings.json'
if (Test-Path -LiteralPath $settingsPath) {
try { Get-Content -LiteralPath $settingsPath -Raw | ConvertFrom-Json | Out-Null; Result 'PASS' 'installed .claude/settings.json is valid JSON' }
catch { Result 'FAIL' 'installed .claude/settings.json is not valid JSON' }
}
$configTomlPath = Join-Path $homePath '.codex/config.toml'
if (Test-Path -LiteralPath $configTomlPath) {
if (-not $python) { Result 'WARN' 'Python unavailable; skipped installed .codex/config.toml TOML check' }
else {
$parseResult = Invoke-DoctorCommand -Command $python.Path -Arguments (@($python.Arguments) + @('-c', "import sys,tomllib,pathlib; tomllib.loads(pathlib.Path(sys.argv[1]).read_text(encoding='utf-8-sig'))", $configTomlPath))
if ($parseResult.ExitCode -eq 0) { Result 'PASS' 'installed .codex/config.toml parses as TOML' } else { Result 'FAIL' "installed .codex/config.toml is invalid TOML: $($parseResult.Output -join ' ')" }
}
if (Get-Command codex -ErrorAction SilentlyContinue) {
$previousCodexHome = $env:CODEX_HOME
try {
$env:CODEX_HOME = Split-Path $configTomlPath -Parent
$schemaResult = Invoke-DoctorCommand -Command 'codex' -Arguments @('--strict-config', '--help')
if ($schemaResult.ExitCode -eq 0) { Result 'PASS' 'installed .codex/config.toml matches the installed Codex schema' } else { Result 'FAIL' "installed .codex/config.toml has unsupported Codex settings: $($schemaResult.Output -join ' ')" }
} finally { $env:CODEX_HOME = $previousCodexHome }
} else { Result 'WARN' 'Codex CLI unavailable; skipped installed Codex schema validation' }
}
foreach ($destination in @((Join-Path $homePath '.codex'), (Join-Path $homePath '.agents/skills'), (Join-Path $homePath '.claude'))) {
$manifestPath = Get-ManagedManifestPath $destination
if (-not (Test-Path -LiteralPath $manifestPath)) { continue }
$manifest = Read-ManagedManifest $manifestPath
$missing = @()
$modified = @()
foreach ($relative in $manifest.Keys) {
$target = Join-Path $destination $relative
if (-not (Test-Path -LiteralPath $target)) { $missing += $relative; continue }
if ((Get-Sha256Hash $target) -ne $manifest[$relative]) { $modified += $relative }
}
if ($missing.Count) { Result 'WARN' "$destination is missing managed files: $($missing -join ', ')" } else { Result 'PASS' "$destination has no missing managed files" }
if ($modified.Count) { Result 'WARN' "$destination has locally modified managed files: $($modified -join ', ')" } else { Result 'PASS' "$destination has no locally modified managed files" }
}
foreach ($path in @('shared/rules','shared/skills','shared/agents','shared/hooks/scripts')) { if (Test-Path (Join-Path $root $path)) { Result 'PASS' "source $path" } else { Result 'FAIL' "missing $path" } }
foreach ($client in @('codex','claude')) {
$config = if ($client -eq 'codex') { Join-Path $root 'adapters/codex/config/config.toml' } else { Join-Path $root 'adapters/claude/config/settings.json' }
$registered = if ($client -eq 'codex') { (Get-Content $config -Raw) -match '\[\[hooks\.Stop\]\]' } else { (Get-Content $config -Raw) -match '"Stop"\s*:' }
if ($registered) { Result 'PASS' "$client Stop hook is registered" } else { Result 'WARN' "$client ships hook utilities, but no Stop hook is registered" }
}
if ($Summary) {
if ($script:fail) {
Write-Output "Doctor: FAIL | $script:checkCount checks"
exit 1
}
Write-Output "Doctor: PASS | $script:checkCount checks"
} else {
if ($script:fail) { exit 1 }
Write-Output 'PASS doctor'
}
exit 0