moyun-ultimate/install.ps1
moyun af44f4b3c9 feat: 0.5.0 - auto-detect conflicts at install time, yield the permission layer
Instead of removing other people's plugins (which breaks their environment and
cannot be rolled back), this detects and yields.

1. install.ps1 scans every installed bundle before copying, reads the config ids
   each one actually rewrites, and intersects with ours:
     scanned plugins : 19
     hard conflicts  : none
       ~ dsh-purge  shares: system-prompt  (safe)

2. Three modes. auto (default) picks complement when a hard conflict exists,
   full otherwise.

     full        permission layer owned by this plugin
     complement  permission layer yields, prompt layers + 63 skills still apply
     auto        decided by the scan

3. switch-mode.ps1 flips between them at any time; the full patch is kept as
   cordis.patch.yml.full so switching back is one command.

New file cordis.patch.complement.yml drops the five permission-related config
overrides entirely, so coexistence is structural rather than hopeful.

Deliberately NOT doing: uninstalling other plugins. Reasons are in the README.
2026-11-17 14:55:29 +08:00

226 lines
8.3 KiB
PowerShell

# moyun-ultimate installer (ASCII only: PS 5.1 reads BOM-less UTF-8 as ANSI)
# Idempotent. Backs up package.json before editing. Does NOT restart DSH.
#
# -Mode auto (default) detect conflicts first, then pick full or complement
# -Mode full always take over the permission layer
# -Mode complement never touch the permission layer (yield to other plugins)
[CmdletBinding()]
param(
[string]$Profile = 'desktop',
[string]$DshHome = '',
[ValidateSet('auto', 'full', 'complement')]
[string]$Mode = 'auto',
[switch]$Uninstall,
[switch]$DryRun
)
$ErrorActionPreference = 'Stop'
$Here = Split-Path -Parent $MyInvocation.MyCommand.Path
$PkgName = 'moyun-ultimate'
if (-not $DshHome) {
if ($env:DSH_HOME) { $DshHome = $env:DSH_HOME }
elseif (Test-Path 'G:\DeepSeek_dsh') { $DshHome = 'G:\DeepSeek_dsh' }
elseif (Test-Path (Join-Path $env:USERPROFILE '.dsh')) { $DshHome = Join-Path $env:USERPROFILE '.dsh' }
else { throw 'DSH_HOME not found. Pass -DshHome <path>.' }
}
$profileDir = Join-Path $DshHome "profiles\$Profile"
$pkgJson = Join-Path $profileDir 'package.json'
$plugDir = Join-Path $profileDir "plugins\$PkgName"
$runDir = Join-Path $profileDir "node_modules\$PkgName"
Write-Output "DSH_HOME : $DshHome"
Write-Output "profile : $Profile"
Write-Output "package dir: $profileDir"
if (-not (Test-Path $pkgJson)) { throw "profile package.json not found: $pkgJson" }
function Get-Pkg { Get-Content -LiteralPath $pkgJson -Raw -Encoding UTF8 | ConvertFrom-Json }
# ---- config ids this plugin rewrites (must match index.js CONFLICT_IDS) ----
$CONFLICT_IDS = @('sandbox-policy', 'approval', 'permission', 'spill-policy', 'tool-result-pruner')
$SOFT_IDS = @('system-prompt')
# Read the config ids another installed plugin actually rewrites.
function Get-PluginIds([string]$name) {
$p = Join-Path $profileDir "node_modules\$name\cordis.patch.yml"
if (-not (Test-Path $p)) { return @() }
$txt = Get-Content -LiteralPath $p -Raw -Encoding UTF8
$ids = @()
foreach ($m in [regex]::Matches($txt, '(?m)^\s*-?\s*id:\s*([A-Za-z0-9_\-]+)')) {
$ids += $m.Groups[1].Value
}
return ($ids | Sort-Object -Unique)
}
# Scan all bundles and report who collides with us.
function Get-ConflictReport {
$rep = @{ hard = @(); soft = @(); scanned = 0 }
$j = Get-Pkg
$bundles = @($j.dsh.profile.bundles)
foreach ($b in $bundles) {
if ($b -eq $PkgName) { continue }
$ids = Get-PluginIds $b
if ($ids.Count -eq 0) { continue }
$rep.scanned++
$hardHit = @($ids | Where-Object { $CONFLICT_IDS -contains $_ })
$softHit = @($ids | Where-Object { $SOFT_IDS -contains $_ })
if ($hardHit.Count -gt 0) {
$rep.hard += [pscustomobject]@{ plugin = $b; ids = ($hardHit -join ',') }
} elseif ($softHit.Count -gt 0) {
$rep.soft += [pscustomobject]@{ plugin = $b; ids = ($softHit -join ',') }
}
}
return $rep
}
function Show-ConflictReport($rep) {
Write-Output ''
Write-Output '--- conflict scan ---'
Write-Output (" scanned plugins : " + $rep.scanned)
if ($rep.hard.Count -eq 0) {
Write-Output ' hard conflicts : none'
} else {
Write-Output ' hard conflicts : YES'
foreach ($h in $rep.hard) { Write-Output (" - " + $h.plugin + " rewrites: " + $h.ids) }
}
if ($rep.soft.Count -eq 0) {
Write-Output ' soft overlaps : none'
} else {
foreach ($s in $rep.soft) { Write-Output (" ~ " + $s.plugin + " shares: " + $s.ids + " (safe)") }
}
}
# Decide the effective mode for 'auto'.
function Resolve-Mode($rep) {
if ($Mode -ne 'auto') { return $Mode }
if ($rep.hard.Count -gt 0) { return 'complement' }
return 'full'
}
# Write the right bundle patch into the installed copies.
function Apply-Mode([string]$effective) {
$fullSrc = Join-Path $Here 'cordis.patch.yml'
$compSrc = Join-Path $Here 'cordis.patch.complement.yml'
foreach ($d in @($plugDir, $runDir)) {
if (-not (Test-Path $d)) { continue }
$target = Join-Path $d 'cordis.patch.yml'
$fullKeep = Join-Path $d 'cordis.patch.yml.full'
if ($effective -eq 'complement') {
if ($DryRun) { Write-Output "would switch $d to COMPLEMENT mode" }
else {
if ((Test-Path $target) -and -not (Test-Path $fullKeep)) { Copy-Item $target $fullKeep -Force }
Copy-Item $compSrc $target -Force
Write-Output " switched to COMPLEMENT mode: $d"
}
} else {
if ($DryRun) { Write-Output "would switch $d to FULL mode" }
else {
Copy-Item $fullSrc $target -Force
Write-Output " switched to FULL mode: $d"
}
}
}
}
if ($Uninstall) {
Write-Output '--- uninstall ---'
foreach ($d in @($plugDir, $runDir)) {
if (Test-Path $d) {
if ($DryRun) { Write-Output "would remove $d" }
else { Remove-Item $d -Recurse -Force; Write-Output "removed $d" }
}
}
$raw = Get-Content -LiteralPath $pkgJson -Raw -Encoding UTF8
$keep = @()
foreach ($l in ($raw -split "`n")) {
if ($l -match "`"$PkgName`":\s*`"file:") { continue }
if ($l -match "^\s*`"$PkgName`",\s*$") { continue }
$keep += $l
}
if ($DryRun) { Write-Output 'would rewrite package.json' }
else {
Copy-Item $pkgJson "$pkgJson.bak-$PkgName-$(Get-Date -Format 'yyyyMMdd-HHmmss')" -Force
[IO.File]::WriteAllText($pkgJson, ($keep -join "`n"), (New-Object Text.UTF8Encoding($false)))
$null = Get-Pkg
Write-Output 'package.json updated'
}
Write-Output 'Restart DSH completely to unload.'
exit 0
}
Write-Output ''
Write-Output '=== install ==='
# 0) scan BEFORE copying, so we can decide the mode
$rep = Get-ConflictReport
Show-ConflictReport $rep
$effective = Resolve-Mode $rep
Write-Output ''
Write-Output ("requested mode : " + $Mode)
Write-Output ("effective mode : " + $effective)
if ($effective -eq 'complement') {
Write-Output ' -> permission layer will YIELD to the plugins listed above.'
Write-Output ' prompt layers + 63 skills still apply.'
}
if ($DryRun) {
Write-Output ''
Write-Output "would copy $Here -> $plugDir"
Write-Output "would copy $Here -> $runDir"
Write-Output "would write $effective bundle patch"
Write-Output 'would add dependencies + bundles entry'
exit 0
}
# 1) source + runtime copy (real dirs: mklink needs elevation)
foreach ($d in @($plugDir, $runDir)) {
if (Test-Path $d) { Remove-Item $d -Recurse -Force }
New-Item -ItemType Directory -Force -Path $d | Out-Null
Copy-Item "$Here\*" $d -Recurse -Force
}
Write-Output "copied to $plugDir"
Write-Output "copied to $runDir"
# 2) write the bundle patch matching the effective mode
Apply-Mode $effective
# 3) patch package.json
$stamp = Get-Date -Format 'yyyyMMdd-HHmmss'
Copy-Item $pkgJson "$pkgJson.bak-$PkgName-$stamp" -Force
Write-Output "backup: package.json.bak-$PkgName-$stamp"
$raw = Get-Content -LiteralPath $pkgJson -Raw -Encoding UTF8
$out = New-Object System.Collections.Generic.List[string]
$depAdded = $false
$bundleHandled = $false
foreach ($l in ($raw -split "`n")) {
if (-not $depAdded -and $l -match '^\s*"dependencies":\s*\{') {
$out.Add($l)
$out.Add(" `"$PkgName`": `"file:./plugins/$PkgName`",")
$depAdded = $true
continue
}
if (-not $bundleHandled -and $l -match '^\s*"moyun-[a-z]+",\s*$') {
$out.Add(" `"$PkgName`",")
$bundleHandled = $true
continue
}
$out.Add($l)
}
[IO.File]::WriteAllText($pkgJson, ($out -join "`n"), (New-Object Text.UTF8Encoding($false)))
$j = Get-Pkg
$hasDep = $j.dependencies.PSObject.Properties.Name -contains $PkgName
$hasBundle = $j.dsh.profile.bundles -contains $PkgName
Write-Output "dependencies has ${PkgName}: $hasDep"
Write-Output "bundles has ${PkgName} : $hasBundle"
Write-Output "bundles total : $($j.dsh.profile.bundles.Count)"
if (-not $hasBundle) {
Write-Warning "bundles entry missing. Add `"$PkgName`" to dsh.profile.bundles manually."
}
Write-Output ''
Write-Output ("DONE ($effective mode). Fully quit DSH and reopen it.")
Write-Output 'Switch modes later: powershell -File .\switch-mode.ps1 -Mode full|complement'
Write-Output 'Verify after restart: powershell -File .\doctor.ps1'