跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

浏览文件

windows-maintenance.ps1

112 行 · 8.0 KB历史下载
1# Windows filesystem regression. Hyper-V, task and service calls are substitutes.
2param([Parameter(Mandatory)][string]$Updater,[Parameter(Mandatory)][string]$ProbeExecutable)
3$ErrorActionPreference='Stop'
4$root=Join-Path ([IO.Path]::GetTempPath()) ('cage-maintenance-test-'+[guid]::NewGuid().ToString('N'))
5$null=New-Item -ItemType Directory -Path $root
6[IO.File]::WriteAllText((Join-Path $root 'owner'),'Cage isolated maintenance regression')
7$previousProgramFiles=$env:ProgramFiles
8$env:ProgramFiles=$root
9$vm=[guid]::NewGuid()
10$sid=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
11$bridge=Join-Path $root ('Cage Guest Bridge\'+$vm)
12$network=Join-Path $root ('Cage Guest Network\'+$vm)
13$null=New-Item -ItemType Directory -Path $bridge,$network
14$old=Join-Path $bridge 'helper.txt'
15[IO.File]::WriteAllText($old,'old helper')
16$account=Join-Path $root 'account-marker'
17[IO.File]::WriteAllText($account,'account and project data')
18$gui="Cage-GUI-Bridge-$vm-$sid"
19$packet="Cage-Packet-Bridge-$vm"
20$global:CageTestTasks=@{}
21$global:CageTestTasks[$gui]=[pscustomobject]@{Description="Cage protected GUI bridge $vm $sid";State='Running';Actions=@([pscustomobject]@{Execute=(Join-Path $bridge 'Cage.GuestClipboard.exe')})}
22$global:CageTestTasks[$packet]=[pscustomobject]@{Description="Cage protected packet bridge $vm $sid";State='Running';Actions=@([pscustomobject]@{Execute=(Join-Path $network 'Cage.GuestNetwork.exe')})}
23function Get-ItemProperty { param($Path) [pscustomobject]@{VirtualMachineId=$vm} }
24function Get-ScheduledTask { param($TaskName) $global:CageTestTasks[$TaskName] }
25function Stop-ScheduledTask { param($TaskName) $global:CageTestTasks[$TaskName].State='Ready' }
26function Start-ScheduledTask { param($TaskName) $global:CageTestTasks[$TaskName].State='Running' }
27function Get-CimInstance { param($ClassName,$Filter) $null }
28function Assert-Value($Condition,[string]$Message) { if (!$Condition) { throw $Message } }
29function New-TestPackage([switch]$WithTools) {
30 $stage=Join-Path $root ('Cage Component Update\'+[guid]::NewGuid().ToString('N'))
31 $source=Join-Path $root ([guid]::NewGuid().ToString('N'))
32 $null=New-Item -ItemType Directory -Path $stage,(Join-Path $source 'files/bridge'),(Join-Path $source 'files/network')
33 [IO.File]::WriteAllText((Join-Path $source 'files/bridge/helper.txt'),'new helper')
34 Copy-Item -LiteralPath $ProbeExecutable -Destination (Join-Path $source 'files/bridge/Cage.GuestClipboard.exe')
35 Copy-Item -LiteralPath $ProbeExecutable -Destination (Join-Path $source 'files/network/Cage.GuestNetwork.exe')
36 $entries=@()
37 foreach ($name in @('bridge/helper.txt','bridge/Cage.GuestClipboard.exe','network/Cage.GuestNetwork.exe')) {
38 $digest=(Get-FileHash -LiteralPath (Join-Path $source ('files/'+$name))).Hash.ToLowerInvariant()
39 $entries+=@{source=('payload/'+[IO.Path]::GetFileName($name));target=$name;sha256=$digest;installed_sha256=$digest}
40 }
41 @{protocol=1;system='windows-11';revision=('a'*64);files=$entries}|ConvertTo-Json -Depth 6 | Set-Content -LiteralPath (Join-Path $source 'native-components.json') -Encoding UTF8
42 if ($WithTools) {
43 $runtime=Join-Path $root ([guid]::NewGuid().ToString('N'))
44 $toolStage=Join-Path $source 'tools'
45 $null=New-Item -ItemType Directory -Path $runtime,$toolStage
46 foreach ($name in @('playwright','vscode-cli','vscode-server')) {
47 $null=New-Item -ItemType Directory -Path (Join-Path $runtime $name)
48 [IO.File]::WriteAllText((Join-Path $runtime ($name+'/fixture.txt')),'new runtime')
49 }
50 foreach ($name in @('node.exe','editor-runtime.json')) { [IO.File]::WriteAllText((Join-Path $runtime $name),'new runtime') }
51 & (Join-Path $env:SystemRoot 'System32/tar.exe') -a -cf (Join-Path $toolStage 'extras.zip') -C $runtime .
52 if ($LASTEXITCODE) { throw 'fixture runtime archive failed' }
53 $installer=@'
54param($VMId,$GUIUserSID,$Bridge,$Stage,$PackagesJSON,$CLIJSON,$ExtrasSHA,$Phase,[switch]$RuntimeInstalled)
55if ($Phase -eq 'Prepare' -and !$RuntimeInstalled) { throw 'fixture received unpacked runtime twice' }
56if ($Phase -eq 'UserTools' -and $env:CAGE_TEST_TOOL_FAILURE) { throw 'simulated package installation failure' }
57'@
58 [IO.File]::WriteAllText((Join-Path $stage 'install-windows-workbench-components.ps1'),$installer)
59 }
60 $archive=Join-Path $stage 'update.zip'
61 & (Join-Path $env:SystemRoot 'System32/tar.exe') -a -cf $archive -C $source .
62 if ($LASTEXITCODE) { throw 'fixture archive failed' }
63 return @{Stage=$stage;Hash=(Get-FileHash -LiteralPath $archive).Hash}
64}
65try {
66 $package=New-TestPackage
67 $env:CAGE_TEST_FAIL_SELF='1'
68 $failed=$false
69 try { & $Updater $vm $sid $package.Stage $package.Hash $false '{}' '{}' } catch { $failed=$true }
70 Assert-Value $failed 'failed update unexpectedly succeeded'
71 Assert-Value ([IO.File]::ReadAllText($old) -ceq 'old helper') 'rollback lost old component'
72 Assert-Value (!(Test-Path -LiteralPath (Join-Path $bridge 'native-components.json'))) 'failed update published receipt'
73 Assert-Value (!(Test-Path -LiteralPath (Join-Path $network 'Cage.GuestNetwork.exe'))) 'rollback left new network agent'
74 Assert-Value ($global:CageTestTasks[$gui].State -eq 'Running') 'rollback did not restore task state'
75 Remove-Item Env:CAGE_TEST_FAIL_SELF
76 for ($attempt=0;$attempt -lt 2;$attempt++) {
77 $package=New-TestPackage
78 & $Updater $vm $sid $package.Stage $package.Hash $false '{}' '{}'
79 Assert-Value ([IO.File]::ReadAllText($old) -ceq 'new helper') 'retry did not install helper'
80 $receipt=Get-Content -LiteralPath (Join-Path $bridge 'native-components.json') -Raw | ConvertFrom-Json
81 Assert-Value ($receipt.revision -ceq ('a'*64)) 'receipt did not follow installed files'
82 Assert-Value ([IO.File]::ReadAllText($account) -ceq 'account and project data') 'personal data changed'
83 $acl=Get-Acl -LiteralPath (Join-Path $bridge 'native-components.json')
84 Assert-Value $acl.AreAccessRulesProtected 'receipt permissions inherited'
85 }
86 $package=New-TestPackage
87 $failed=$false
88 try { & $Updater $vm $sid $package.Stage ('0'*64) $false '{}' '{}' } catch { $failed=$true }
89 Assert-Value $failed 'damaged transfer accepted'
90 Assert-Value ([IO.File]::ReadAllText($old) -ceq 'new helper') 'rejected transfer changed installed files'
91 $editor=Join-Path $bridge 'vscode-server'
92 $null=New-Item -ItemType Directory -Path $editor
93 [IO.File]::WriteAllText((Join-Path $editor 'fixture.txt'),'original runtime')
94 $env:CAGE_TEST_TOOL_FAILURE='1'
95 $package=New-TestPackage -WithTools
96 $failed=$false
97 try { & $Updater $vm $sid $package.Stage $package.Hash $true '{}' '{}' } catch { $failed=$_.Exception.Message -ceq 'simulated package installation failure' }
98 Assert-Value $failed 'fixture did not reach package installation failure'
99 Assert-Value ([IO.File]::ReadAllText((Join-Path $editor 'fixture.txt')) -ceq 'original runtime') 'old editor was not restored'
100 Assert-Value (!(Test-Path -LiteralPath (Join-Path $bridge 'node.exe'))) 'failure published an entirely new runtime'
101 Remove-Item Env:CAGE_TEST_TOOL_FAILURE
102 $package=New-TestPackage -WithTools
103 & $Updater $vm $sid $package.Stage $package.Hash $true '{}' '{}'
104 Assert-Value ([IO.File]::ReadAllText((Join-Path $editor 'fixture.txt')) -ceq 'new runtime') 'tool retry did not replace the editor'
105 [pscustomobject]@{Rollback=$true;Retry=$true;RepeatedUpdate=$true;Manifest=$true;Permissions=$true;PersonalDataPreserved=$true;DamagedTransferRejected=$true;ToolRuntimeRollbackAndRetry=$true;RealWindowsFilesystem=$true;HyperVAndServicesMocked=$true} | ConvertTo-Json -Compress
106} finally {
107 $env:ProgramFiles=$previousProgramFiles
108 Remove-Item Env:CAGE_TEST_FAIL_SELF -ErrorAction SilentlyContinue
109 Remove-Item Env:CAGE_TEST_TOOL_FAILURE -ErrorAction SilentlyContinue
110 Remove-Variable CageTestTasks -Scope Global -ErrorAction SilentlyContinue
111 if ([IO.File]::ReadAllText((Join-Path $root 'owner')) -ceq 'Cage isolated maintenance regression') { Remove-Item -LiteralPath $root -Recurse -Force }
112}