@simplexai/localmodels 0.0.0-stage → 0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,8 @@
1
1
  {
2
2
  "name": "@simplexai/localmodels",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c",
4
+ "license": "UNLICENSED",
5
+ "files": [
6
+ "**/*"
7
+ ]
8
+ }
package/release.json ADDED
@@ -0,0 +1,115 @@
1
+ {
2
+ "baseUrl": "https://d2ikbbcl3j6jz9.cloudfront.net/cli/localmodels/0.1.0.dev28",
3
+ "kits": {
4
+ "darwin-arm64": {
5
+ "bootstrap": {
6
+ "entries": 2029,
7
+ "files": 1865,
8
+ "inventorySha256": "c3c67cef554d1a0561ff613e8caa920b80144305dc373465cf36e7733d7f5dfb",
9
+ "moduleDir": "modules",
10
+ "python": "python/bin/python3.12"
11
+ },
12
+ "bytes": 40712386,
13
+ "manifestSha256": "e1f3fa3771acc33661515c1d977f5fb43017ed9d2c9defe7f31ea16acc031394",
14
+ "sha256": "f9e113ce48586d4076837647ddcf74b7e17a885e46026078a651ff9417ebe53d",
15
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
16
+ "url": "https://registry.npmjs.org/@simplexai/localmodels-darwin-arm64/-/localmodels-darwin-arm64-0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c.tgz"
17
+ },
18
+ "darwin-x64": {
19
+ "bootstrap": {
20
+ "entries": 2025,
21
+ "files": 1862,
22
+ "inventorySha256": "0a942e3afa4e4825ae10fe6051a3df327294545c084ad88c774d940c92dc2ecc",
23
+ "moduleDir": "modules",
24
+ "python": "python/bin/python3.12"
25
+ },
26
+ "bytes": 40193280,
27
+ "manifestSha256": "cb8e632424cf8ffe3d15c9fd1d2df1f3415c6e114ebba7cdb2139c6a8f1986d7",
28
+ "sha256": "22d0eec6661ccddd71bb55f4bc060ef557ac610471e37ae1b157244f49c184c9",
29
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
30
+ "url": "https://registry.npmjs.org/@simplexai/localmodels-darwin-x64/-/localmodels-darwin-x64-0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c.tgz"
31
+ },
32
+ "linux-arm64": {
33
+ "bootstrap": {
34
+ "entries": 4943,
35
+ "files": 4736,
36
+ "inventorySha256": "59e9f7d91dee72006b36402b951ca9a0189d14d8846019e009d1f2461e6559d9",
37
+ "moduleDir": "modules",
38
+ "python": "python/bin/python3.12"
39
+ },
40
+ "bytes": 55658788,
41
+ "manifestSha256": "04efa34d89f5e851c2aae73c24e2e90c571774e5b059432cbeb1fb7676e5d784",
42
+ "sha256": "ad0d8d1814213bb7654f192e8a7a2231c33d6fc66c264b36c47ea38573cac00a",
43
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
44
+ "url": "https://registry.npmjs.org/@simplexai/localmodels-linux-arm64/-/localmodels-linux-arm64-0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c.tgz"
45
+ },
46
+ "linux-x64": {
47
+ "bootstrap": {
48
+ "entries": 4943,
49
+ "files": 4736,
50
+ "inventorySha256": "1033dae80313953edca873f32816e3ee7846da6ba0269edd54904f6eba2c1faa",
51
+ "moduleDir": "modules",
52
+ "python": "python/bin/python3.12"
53
+ },
54
+ "bytes": 68864113,
55
+ "manifestSha256": "58b7a5e8befec1f5a884821e45221d52c5914a261178a0efa6b2cb71e4e629a0",
56
+ "sha256": "91227931e395cc4a5d383d3d1df7ee26d7df60ac47102d025c3b7ea938e859d1",
57
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
58
+ "url": "https://registry.npmjs.org/@simplexai/localmodels-linux-x64/-/localmodels-linux-x64-0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c.tgz"
59
+ },
60
+ "win32-x64": {
61
+ "bootstrap": {
62
+ "entries": 3817,
63
+ "files": 3548,
64
+ "inventorySha256": "f220454ed8e900b54235625fe36ec09dd112d53dc3020ca6751d2ab16034bcc6",
65
+ "moduleDir": "modules",
66
+ "python": "python/python.exe"
67
+ },
68
+ "bytes": 23101669,
69
+ "manifestSha256": "b78c5ae01113534ef955a9d5715aa54ac0d0b4f2f967febaa49385e6681d5c4a",
70
+ "sha256": "41d03a9baf14ada14da854e6a24ebefc09d1eb423792f7ae2d16ed12e03065f1",
71
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
72
+ "url": "https://registry.npmjs.org/@simplexai/localmodels-win32-x64/-/localmodels-win32-x64-0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c.tgz"
73
+ }
74
+ },
75
+ "launchers": {
76
+ "stack.ps1": {
77
+ "bytes": 30718,
78
+ "schema": "localmodels.kit-location/v1",
79
+ "sha256": "d083b75c9ebd08c2e5f88443e90165e5959fac87dd74d54dfe5e276ceab52d59",
80
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
81
+ "targets": [
82
+ "darwin-arm64",
83
+ "darwin-x64",
84
+ "linux-arm64",
85
+ "linux-x64",
86
+ "win32-x64"
87
+ ],
88
+ "url": "https://cdn.jsdelivr.net/npm/@simplexai/localmodels@0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c/stack.ps1",
89
+ "version": "0.1.0.dev28"
90
+ },
91
+ "stack.sh": {
92
+ "bytes": 15275,
93
+ "schema": "localmodels.kit-location/v1",
94
+ "sha256": "2c080211b73299faa6f209d94be2accb60270c45406300acf06302cfc8a3a263",
95
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
96
+ "targets": [
97
+ "darwin-arm64",
98
+ "darwin-x64",
99
+ "linux-arm64",
100
+ "linux-x64"
101
+ ],
102
+ "url": "https://cdn.jsdelivr.net/npm/@simplexai/localmodels@0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c/stack.sh",
103
+ "version": "0.1.0.dev28"
104
+ }
105
+ },
106
+ "npmVersion": "0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c",
107
+ "schemaVersion": 1,
108
+ "sourceCommit": "3e948cdda88604db5ec65f477afac627bf69db2c",
109
+ "version": "0.1.0.dev28",
110
+ "wheel": {
111
+ "bytes": 1247184,
112
+ "file": "simplex_localmodels-0.1.0.dev28-py3-none-any.whl",
113
+ "sha256": "72d9fc155362fe6cb3e65a0101efe9dc2305bb1747a572deb86d46689f00e94b"
114
+ }
115
+ }
package/stack.ps1 ADDED
@@ -0,0 +1,588 @@
1
+ Set-StrictMode -Version 3.0
2
+ $ErrorActionPreference = 'Stop'
3
+ $ProgressPreference = 'SilentlyContinue'
4
+
5
+ # >>> release pin
6
+ $StackVersion = '0.1.0.dev28'
7
+ $StackSourceCommit = '3e948cdda88604db5ec65f477afac627bf69db2c'
8
+ $StackNpmVersion = '0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c'
9
+ $StackKits = @{
10
+ 'darwin-arm64' = 'f9e113ce48586d4076837647ddcf74b7e17a885e46026078a651ff9417ebe53d'
11
+ 'darwin-x64' = '22d0eec6661ccddd71bb55f4bc060ef557ac610471e37ae1b157244f49c184c9'
12
+ 'linux-arm64' = 'ad0d8d1814213bb7654f192e8a7a2231c33d6fc66c264b36c47ea38573cac00a'
13
+ 'linux-x64' = '91227931e395cc4a5d383d3d1df7ee26d7df60ac47102d025c3b7ea938e859d1'
14
+ 'win32-x64' = '41d03a9baf14ada14da854e6a24ebefc09d1eb423792f7ae2d16ed12e03065f1'
15
+ }
16
+ $StackBootstrap = @{
17
+ 'darwin-arm64' = @{
18
+ manifest = 'e1f3fa3771acc33661515c1d977f5fb43017ed9d2c9defe7f31ea16acc031394'
19
+ inventory = 'c3c67cef554d1a0561ff613e8caa920b80144305dc373465cf36e7733d7f5dfb'
20
+ bytes = '40712386'
21
+ files = '1865'
22
+ entries = '2029'
23
+ python = 'python/bin/python3.12'
24
+ modules = 'modules'
25
+ }
26
+ 'darwin-x64' = @{
27
+ manifest = 'cb8e632424cf8ffe3d15c9fd1d2df1f3415c6e114ebba7cdb2139c6a8f1986d7'
28
+ inventory = '0a942e3afa4e4825ae10fe6051a3df327294545c084ad88c774d940c92dc2ecc'
29
+ bytes = '40193280'
30
+ files = '1862'
31
+ entries = '2025'
32
+ python = 'python/bin/python3.12'
33
+ modules = 'modules'
34
+ }
35
+ 'linux-arm64' = @{
36
+ manifest = '04efa34d89f5e851c2aae73c24e2e90c571774e5b059432cbeb1fb7676e5d784'
37
+ inventory = '59e9f7d91dee72006b36402b951ca9a0189d14d8846019e009d1f2461e6559d9'
38
+ bytes = '55658788'
39
+ files = '4736'
40
+ entries = '4943'
41
+ python = 'python/bin/python3.12'
42
+ modules = 'modules'
43
+ }
44
+ 'linux-x64' = @{
45
+ manifest = '58b7a5e8befec1f5a884821e45221d52c5914a261178a0efa6b2cb71e4e629a0'
46
+ inventory = '1033dae80313953edca873f32816e3ee7846da6ba0269edd54904f6eba2c1faa'
47
+ bytes = '68864113'
48
+ files = '4736'
49
+ entries = '4943'
50
+ python = 'python/bin/python3.12'
51
+ modules = 'modules'
52
+ }
53
+ 'win32-x64' = @{
54
+ manifest = 'b78c5ae01113534ef955a9d5715aa54ac0d0b4f2f967febaa49385e6681d5c4a'
55
+ inventory = 'f220454ed8e900b54235625fe36ec09dd112d53dc3020ca6751d2ab16034bcc6'
56
+ bytes = '23101669'
57
+ files = '3548'
58
+ entries = '3817'
59
+ python = 'python/python.exe'
60
+ modules = 'modules'
61
+ }
62
+ }
63
+ # <<< release pin
64
+
65
+ function Stop-Stack([string]$Message) {
66
+ $failure = [InvalidOperationException]::new($Message)
67
+ $failure.Data['StackError'] = $true
68
+ throw $failure
69
+ }
70
+ function Quote-Native([string]$Value) {
71
+ return '"' + [regex]::Replace([regex]::Replace($Value, '(\\*)"', '$1$1\"'), '(\\+)$', '$1$1') + '"'
72
+ }
73
+ function Invoke-Native([string]$File, [string[]]$Arguments) {
74
+ $start = New-Object Diagnostics.ProcessStartInfo
75
+ $start.FileName = $File
76
+ $start.UseShellExecute = $false
77
+ if ($start.PSObject.Properties['ArgumentList']) {
78
+ foreach ($argument in $Arguments) { $start.ArgumentList.Add($argument) }
79
+ } else {
80
+ $start.Arguments = (($Arguments | ForEach-Object { Quote-Native $_ }) -join ' ')
81
+ }
82
+ $process = New-Object Diagnostics.Process
83
+ $process.StartInfo = $start
84
+ $started = $false
85
+ try {
86
+ $started = $process.Start()
87
+ while (-not $process.WaitForExit(100)) { }
88
+ return $process.ExitCode
89
+ } finally {
90
+ if ($started -and -not $process.HasExited) { $process.Kill() }
91
+ $process.Dispose()
92
+ }
93
+ }
94
+ function Get-Digest([string]$Path) {
95
+ return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
96
+ }
97
+ function Assert-Relative([string]$Name) {
98
+ if ($Name -cnotmatch '^[A-Za-z0-9_+./-]+$' -or $Name.StartsWith('/') -or
99
+ @($Name.Split('/') | Where-Object {
100
+ $_ -in @('', '.', '..') -or $_.EndsWith('.') -or
101
+ $_ -imatch '^(CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])(\.|$)'
102
+ }).Count) { Stop-Stack 'unsafe kit relative path' }
103
+ }
104
+ function Assert-Item([string]$Path, [bool]$Directory) {
105
+ $item = Get-Item -LiteralPath $Path -Force
106
+ if (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -or ($item -is [IO.DirectoryInfo]) -ne $Directory) {
107
+ Stop-Stack 'unsafe kit path'
108
+ }
109
+ return $item
110
+ }
111
+ function Assert-WindowsPrivate([string]$Path, [bool]$Repair, [bool]$AllowInherited = $false) {
112
+ $item = Get-Item -LiteralPath $Path -Force
113
+ $sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
114
+ $system = [Security.Principal.SecurityIdentifier]::new('S-1-5-18')
115
+ $admins = 'S-1-5-32-544'
116
+ $trustedInstaller = 'S-1-5-80-956008885-3418522649-1831038044-1853292631-2271478464'
117
+ if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { Stop-Stack 'linked private path' }
118
+ $acl = Get-Acl -LiteralPath $Path
119
+ $owner = $acl.GetOwner([Security.Principal.SecurityIdentifier]).Value
120
+ if ($owner -ne $sid.Value -and -not ($Repair -and $owner -eq $admins)) { Stop-Stack 'foreign kit owner' }
121
+ $parent = if (($item -is [IO.DirectoryInfo])) { $item.Parent } else { $item.Directory }
122
+ while ($parent) {
123
+ if ($parent.Attributes -band [IO.FileAttributes]::ReparsePoint) { Stop-Stack 'linked private parent' }
124
+ $parentAcl = Get-Acl -LiteralPath $parent.FullName
125
+ if ($parentAcl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sid.Value, $system.Value, $admins, $trustedInstaller)) {
126
+ Stop-Stack 'foreign private parent'
127
+ }
128
+ foreach ($rule in $parentAcl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])) {
129
+ if ($rule.PropagationFlags -band [Security.AccessControl.PropagationFlags]::InheritOnly) { continue }
130
+ if ($rule.AccessControlType -eq 'Allow' -and $rule.IdentityReference.Value -notin @($sid.Value, $system.Value, $admins, 'S-1-3-0', 'S-1-3-4', $trustedInstaller) -and
131
+ ($rule.FileSystemRights -band [Security.AccessControl.FileSystemRights]'Delete,DeleteSubdirectoriesAndFiles,ChangePermissions,TakeOwnership')) {
132
+ Stop-Stack 'unsafe private parent permissions'
133
+ }
134
+ }
135
+ $parent = $parent.Parent
136
+ }
137
+ if ($Repair) {
138
+ $acl = if (($item -is [IO.DirectoryInfo])) { [Security.AccessControl.DirectorySecurity]::new() } else { [Security.AccessControl.FileSecurity]::new() }
139
+ $acl.SetOwner($sid)
140
+ $acl.SetAccessRuleProtection($true, $false)
141
+ foreach ($principal in @($sid, $system)) {
142
+ $rule = if (($item -is [IO.DirectoryInfo])) {
143
+ [Security.AccessControl.FileSystemAccessRule]::new($principal, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')
144
+ } else { [Security.AccessControl.FileSystemAccessRule]::new($principal, 'FullControl', 'Allow') }
145
+ $acl.AddAccessRule($rule)
146
+ }
147
+ if ($PSVersionTable.PSEdition -eq 'Core') { [IO.FileSystemAclExtensions]::SetAccessControl($item, $acl) } else { $item.SetAccessControl($acl) }
148
+ $acl = Get-Acl -LiteralPath $Path
149
+ }
150
+ if (($item -is [IO.DirectoryInfo]) -and -not $AllowInherited -and -not $acl.AreAccessRulesProtected) { Stop-Stack 'inherited private permissions' }
151
+ $allowed = $false
152
+ foreach ($rule in $acl.GetAccessRules($true, $true, [Security.Principal.SecurityIdentifier])) {
153
+ if ($rule.AccessControlType -eq 'Allow') {
154
+ if ($rule.IdentityReference.Value -notin @($sid.Value, $system.Value)) { Stop-Stack 'shared kit permissions' }
155
+ if ($rule.IdentityReference.Value -eq $sid.Value) { $allowed = $true }
156
+ }
157
+ }
158
+ if (-not $allowed) { Stop-Stack 'missing owner access' }
159
+ }
160
+ function Assert-PosixMode([string]$Path, [string[]]$Modes = @()) {
161
+ $info = if ($IsMacOS) { & stat -f '%u %Lp' $Path } else { & stat -c '%u %a' -- $Path }
162
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not inspect kit permissions' }
163
+ $parts = $info.Trim().Split(' ')
164
+ if ($parts[0] -ne $script:Owner -or ($Modes.Count -gt 0 -and $parts[1] -notin $Modes)) { Stop-Stack 'kit must be private, owned and sealed' }
165
+ }
166
+ function Assert-Root {
167
+ $null = Assert-Item $root $true
168
+ if ($onWindows) { Assert-WindowsPrivate $root $false } else {
169
+ Assert-PosixMode $root @('700', '500')
170
+ $ancestor = [IO.Directory]::GetParent($root)
171
+ while ($ancestor) {
172
+ $null = Assert-Item $ancestor.FullName $true
173
+ $info = if ($IsMacOS) { & stat -f '%u %Lp' $ancestor.FullName } else { & stat -c '%u %a' -- $ancestor.FullName }
174
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not inspect private parent' }
175
+ $parts = $info.Trim().Split(' ')
176
+ $mode = [Convert]::ToInt32($parts[1], 8)
177
+ if ($parts[0] -notin @($script:Owner, '0') -or (($mode -band 18) -and -not ($mode -band 512))) {
178
+ Stop-Stack 'unsafe private parent permissions'
179
+ }
180
+ $ancestor = $ancestor.Parent
181
+ }
182
+ }
183
+ }
184
+ function Get-KitTree([string]$Directory) {
185
+ $queue = New-Object 'Collections.Generic.Queue[IO.FileSystemInfo]'
186
+ $queue.Enqueue((Assert-Item $Directory $true))
187
+ $count = 0
188
+ while ($queue.Count) {
189
+ $item = $queue.Dequeue()
190
+ if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { Stop-Stack 'linked kit path' }
191
+ $count++
192
+ if ($count -gt [int]$pin.entries) { Stop-Stack 'kit tree exceeds entry limit' }
193
+ $item
194
+ if (($item -is [IO.DirectoryInfo])) {
195
+ foreach ($child in Get-ChildItem -LiteralPath $item.FullName -Force) { $queue.Enqueue($child) }
196
+ }
197
+ }
198
+ }
199
+ function Assert-Kit([string]$Directory) {
200
+ $tree = @(Get-KitTree $Directory)
201
+ $payload = @($tree | Where-Object { $_ -isnot [IO.DirectoryInfo] })
202
+ if ($tree.Count -ne [int]$pin.entries -or $payload.Count -ne [int]$pin.files) { Stop-Stack 'kit tree mismatch' }
203
+ foreach ($item in $tree) {
204
+ if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { Stop-Stack 'linked kit path' }
205
+ if ($onWindows) {
206
+ Assert-WindowsPrivate $item.FullName $false
207
+ } else {
208
+ $modes = if (($item -is [IO.DirectoryInfo])) { @('500') } else { @('400', '500') }
209
+ Assert-PosixMode $item.FullName $modes
210
+ }
211
+ }
212
+ if (-not $onWindows) { Assert-PosixMode (Join-Path $Directory $pin.python) @('500') }
213
+ $inventory = Join-Path $Directory 'bootstrap.sha256'
214
+ $manifest = Join-Path $Directory 'tool.json'
215
+ if ((Get-Digest $inventory) -ne $pin.inventory -or (Get-Digest $manifest) -ne $pin.manifest) { Stop-Stack 'kit metadata checksum mismatch' }
216
+ $identity = [IO.File]::ReadAllText((Join-Path $Directory 'bootstrap.identity')).TrimEnd("`n")
217
+ if ($identity -cne "$target $StackSourceCommit $StackVersion $($pin.python) $($pin.modules)") { Stop-Stack 'kit identity mismatch' }
218
+ $json = [IO.File]::ReadAllText($manifest)
219
+ $body = if ($PSVersionTable.PSVersion.Major -ge 6) { $json | ConvertFrom-Json -AsHashtable } else { $json | ConvertFrom-Json }
220
+ $assets = [Collections.Generic.Dictionary[string,object]]::new([StringComparer]::Ordinal)
221
+ if ($body.assets -is [Collections.IDictionary]) {
222
+ foreach ($asset in $body.assets.GetEnumerator()) { $assets.Add($asset.Key, $asset.Value) }
223
+ } elseif ($body.assets -is [pscustomobject]) {
224
+ foreach ($asset in $body.assets.PSObject.Properties) { $assets.Add($asset.Name, $asset.Value) }
225
+ } else { Stop-Stack 'invalid manifest assets type' }
226
+ foreach ($field in @('platform', 'arch', 'sourceCommit', 'python', 'moduleDir')) {
227
+ if ($body.$field -isnot [string]) { Stop-Stack 'invalid manifest identity type' }
228
+ }
229
+ if ($body.schemaVersion -isnot [ValueType] -or
230
+ $body.schemaVersion -is [bool] -or $body.schemaVersion -ne 1 -or "$($body.platform)-$($body.arch)" -cne $target -or
231
+ $body.sourceCommit -cne $StackSourceCommit -or $body.python -cne $pin.python -or $body.moduleDir -cne $pin.modules -or
232
+ -not $assets.ContainsKey($pin.python)) { Stop-Stack 'manifest target or source mismatch' }
233
+ $lines = [IO.File]::ReadAllLines($inventory)
234
+ $checks = [Collections.Generic.Dictionary[string,string]]::new([StringComparer]::Ordinal)
235
+ foreach ($line in $lines) {
236
+ if ($line -cnotmatch '^([a-f0-9]{64}) ([A-Za-z0-9_+./-]+)$') { Stop-Stack 'invalid checksum inventory' }
237
+ $expected = $Matches[1]; $relative = $Matches[2]
238
+ Assert-Relative $relative
239
+ if ($checks.ContainsKey($relative)) { Stop-Stack 'duplicate inventory path' }
240
+ $checks.Add($relative, $expected)
241
+ $file = Join-Path $Directory $relative
242
+ $null = Assert-Item $file $false
243
+ if ((Get-Digest $file) -cne $expected) { Stop-Stack 'kit payload checksum mismatch' }
244
+ }
245
+ if ($checks.Count -ne $payload.Count - 1 -or $assets.Count -ne $checks.Count - 2) { Stop-Stack 'manifest asset list mismatch' }
246
+ foreach ($asset in $assets.GetEnumerator()) {
247
+ if ($asset.Value -isnot [string] -or $asset.Key -in @('tool.json', 'bootstrap.identity', 'bootstrap.sha256') -or
248
+ -not $checks.ContainsKey($asset.Key) -or $asset.Value -cne $checks[$asset.Key]) {
249
+ Stop-Stack 'manifest asset checksum mismatch'
250
+ }
251
+ }
252
+ }
253
+ function Set-KitMode([string]$Directory) {
254
+ $tree = @(Get-KitTree $Directory)
255
+ foreach ($item in $tree) {
256
+ if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) { Stop-Stack 'linked kit path' }
257
+ if ($onWindows) {
258
+ Assert-WindowsPrivate $item.FullName $true
259
+ if (-not ($item -is [IO.DirectoryInfo])) { $item.IsReadOnly = $true }
260
+ } else {
261
+ $mode = if (($item -is [IO.DirectoryInfo])) { '500' } else { 'a-w,go-rwx' }
262
+ & chmod $mode -- $item.FullName
263
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not seal kit' }
264
+ }
265
+ }
266
+ }
267
+ function Assert-NpmUrl([string]$Url, [bool]$Redirect = $false) {
268
+ try { $uri = [uri]::new($Url, [UriKind]::Absolute) } catch {
269
+ throw [Net.WebException]::new('invalid npm URL')
270
+ }
271
+ $localFixture = -not $Redirect -and $uri.Scheme -eq 'http' -and $uri.IsLoopback -and
272
+ ($env:SIMPLEX_NPM_REGISTRY -or $env:LOCALMODELS_KIT_BASE_URL)
273
+ if (($uri.Scheme -ne 'https' -and -not $localFixture) -or $uri.UserInfo) {
274
+ throw [Net.WebException]::new('npm URL must use HTTPS without credentials')
275
+ }
276
+ return $uri.AbsoluteUri
277
+ }
278
+ function Get-RedirectUrl([string]$Base, [string]$Location, [int]$Hops) {
279
+ if ($Hops -ge 5 -or -not $Location) { throw [Net.WebException]::new('npm redirect limit or invalid target') }
280
+ try { $next = [uri]::new([uri]$Base, $Location).AbsoluteUri } catch {
281
+ throw [Net.WebException]::new('invalid npm redirect')
282
+ }
283
+ return Assert-NpmUrl $next $true
284
+ }
285
+ function Get-RegistryResponse([string]$Url) {
286
+ if ($PSVersionTable.PSVersion.Major -lt 6) {
287
+ [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
288
+ }
289
+ $Url = Assert-NpmUrl $Url
290
+ for ($hops = 0; $hops -le 5; $hops++) {
291
+ $request = [Net.WebRequest]::Create($Url)
292
+ $request.Timeout = 300000
293
+ $request.ReadWriteTimeout = 300000
294
+ $request.AllowAutoRedirect = $false
295
+ $response = $request.GetResponse()
296
+ if ([int]$response.StatusCode -notin @(301, 302, 303, 307, 308)) { return $response }
297
+ $location = $response.Headers['Location']
298
+ $response.Dispose()
299
+ $Url = Get-RedirectUrl $Url $location $hops
300
+ }
301
+ }
302
+ function Get-Archive([string]$Url, [string]$Destination) {
303
+ $response = $null; $inputStream = $null; $outputStream = $null
304
+ try {
305
+ $response = Get-RegistryResponse $Url
306
+ if ($response.ContentLength -gt [long]$pin.bytes) { Stop-Stack 'archive size exceeds pin' }
307
+ $inputStream = $response.GetResponseStream()
308
+ $outputStream = [IO.File]::Open($Destination, 'CreateNew', 'Write', 'None')
309
+ if ($onWindows) { Assert-WindowsPrivate $Destination $true }
310
+ $buffer = New-Object byte[] 65536
311
+ $total = 0L
312
+ while (($count = $inputStream.Read($buffer, 0, $buffer.Length)) -gt 0) {
313
+ $total += $count
314
+ if ($total -gt [long]$pin.bytes) { Stop-Stack 'archive size exceeds pin' }
315
+ $outputStream.Write($buffer, 0, $count)
316
+ }
317
+ } finally {
318
+ if ($outputStream) { $outputStream.Dispose() }
319
+ if ($inputStream) { $inputStream.Dispose() }
320
+ if ($response) { $response.Dispose() }
321
+ }
322
+ }
323
+ function Get-NpmUrl([string]$Region) {
324
+ $base = if ($Region -eq 'cn') { 'https://registry.npmmirror.com' } else { 'https://registry.npmjs.org' }
325
+ if ($env:SIMPLEX_NPM_REGISTRY) { $base = $env:SIMPLEX_NPM_REGISTRY }
326
+ $name = "localmodels-$target"
327
+ return "$($base.TrimEnd('/'))/@simplexai/$name/-/$name-$StackNpmVersion.tgz"
328
+ }
329
+ function Resolve-Region {
330
+ if ($script:region -eq 'auto' -and $env:LOCALMODELS_KIT_BASE_URL) { $script:region = 'global' }
331
+ if ($script:region -eq 'auto') {
332
+ if ($PSVersionTable.PSVersion.Major -lt 6) {
333
+ [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12
334
+ }
335
+ Add-Type -AssemblyName System.Net.Http
336
+ $handler = [Net.Http.HttpClientHandler]::new()
337
+ $handler.AllowAutoRedirect = $false
338
+ $client = [Net.Http.HttpClient]::new($handler)
339
+ $client.Timeout = [TimeSpan]::FromSeconds(5)
340
+ $client.MaxResponseContentBufferSize = 262144
341
+ $client.DefaultRequestHeaders.Range = [Net.Http.Headers.RangeHeaderValue]::new(0, 262143)
342
+ $tasks = @{}; $urls = @{}; $hops = @{}; $timeouts = @{}
343
+ try {
344
+ foreach ($candidate in @('global', 'cn')) {
345
+ $urls[$candidate] = Assert-NpmUrl (Get-NpmUrl $candidate)
346
+ $hops[$candidate] = 0
347
+ $timeouts[$candidate] = [Threading.CancellationTokenSource]::new(5000)
348
+ $tasks[$candidate] = $client.GetAsync($urls[$candidate], $timeouts[$candidate].Token)
349
+ }
350
+ $script:region = 'global'
351
+ while ($tasks.Count) {
352
+ $finished = [Threading.Tasks.Task]::WhenAny([Threading.Tasks.Task[]]@($tasks.Values)).GetAwaiter().GetResult()
353
+ $candidate = @($tasks.Keys | Where-Object { $tasks[$_] -eq $finished })[0]
354
+ if ($finished.Status -eq 'RanToCompletion') {
355
+ $response = $finished.Result
356
+ try {
357
+ if ($response.IsSuccessStatusCode) {
358
+ $script:region = $candidate
359
+ break
360
+ }
361
+ if ([int]$response.StatusCode -in @(301, 302, 303, 307, 308)) {
362
+ $urls[$candidate] = Get-RedirectUrl $urls[$candidate] "$($response.Headers.Location)" $hops[$candidate]
363
+ $hops[$candidate]++
364
+ $tasks[$candidate] = $client.GetAsync($urls[$candidate], $timeouts[$candidate].Token)
365
+ continue
366
+ }
367
+ } catch [Net.WebException] { } finally { $response.Dispose() }
368
+ }
369
+ $tasks.Remove($candidate)
370
+ }
371
+ } finally {
372
+ $client.CancelPendingRequests()
373
+ $client.Dispose()
374
+ foreach ($timeout in $timeouts.Values) { $timeout.Cancel(); $timeout.Dispose() }
375
+ }
376
+ }
377
+ $hostName = ([uri](Get-NpmUrl $script:region)).Authority
378
+ [Console]::Error.WriteLine("stack.ps1: using npm mirror $hostName ($script:region)")
379
+ }
380
+ function Remove-KitStage([string]$Path, [bool]$AllowLinks = $false) {
381
+ Assert-Root
382
+ $stageRoot = Assert-Item $Path $true
383
+ if ($stageRoot.Parent.FullName -cne $root -or
384
+ $stageRoot.Name -cnotmatch '\A\.staging\.[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\z') {
385
+ Stop-Stack 'unsafe staging path'
386
+ }
387
+ $tree = New-Object 'Collections.Generic.List[IO.FileSystemInfo]'
388
+ $tree.Add($stageRoot)
389
+ for ($index = 0; $index -lt $tree.Count; $index++) {
390
+ $item = $tree[$index]
391
+ if ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) {
392
+ if (-not $AllowLinks) { Stop-Stack 'linked staging path' }
393
+ if ($onWindows) {
394
+ $owner = (Get-Acl -LiteralPath $item.FullName).GetOwner([Security.Principal.SecurityIdentifier]).Value
395
+ if ($owner -ne [Security.Principal.WindowsIdentity]::GetCurrent().User.Value) { Stop-Stack 'foreign staging owner' }
396
+ } else { Assert-PosixMode $item.FullName }
397
+ continue
398
+ }
399
+ if ($onWindows) { Assert-WindowsPrivate $item.FullName $false $true } else { Assert-PosixMode $item.FullName }
400
+ if ($item -is [IO.DirectoryInfo]) {
401
+ foreach ($child in Get-ChildItem -LiteralPath $item.FullName -Force) { $tree.Add($child) }
402
+ }
403
+ }
404
+ if (-not $onWindows) {
405
+ foreach ($item in $tree) {
406
+ if ($item -is [IO.DirectoryInfo] -and -not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {
407
+ & chmod u+w -- $item.FullName
408
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not reopen staging directory' }
409
+ }
410
+ }
411
+ }
412
+ for ($index = $tree.Count - 1; $index -ge 0; $index--) {
413
+ $item = $tree[$index]
414
+ if ($item -is [IO.DirectoryInfo]) { [IO.Directory]::Delete($item.FullName) } else {
415
+ if ($onWindows -and -not ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { $item.IsReadOnly = $false }
416
+ [IO.File]::Delete($item.FullName)
417
+ }
418
+ }
419
+ }
420
+ function Enter-KitLock([int]$Attempts, [int]$DelayMilliseconds) {
421
+ $lockPath = Join-Path $root ".lock-$kit"
422
+ for ($attempt = 0; $attempt -lt $Attempts; $attempt++) {
423
+ if ($onWindows) {
424
+ try {
425
+ return [IO.FileStream]::new($lockPath, [IO.FileMode]::CreateNew, [IO.FileAccess]::Write, [IO.FileShare]::None, 1, [IO.FileOptions]::DeleteOnClose)
426
+ } catch [IO.IOException] { }
427
+ } else {
428
+ & mkdir -- $lockPath 2>$null
429
+ if ($LASTEXITCODE -eq 0) { return $true }
430
+ }
431
+ Start-Sleep -Milliseconds $DelayMilliseconds
432
+ }
433
+ Stop-Stack 'cache lock timeout'
434
+ }
435
+ function Exit-KitLock($Handle) {
436
+ if ($onWindows) { $Handle.Dispose() } else { [IO.Directory]::Delete((Join-Path $root ".lock-$kit")) }
437
+ }
438
+ function Remove-OperationStage {
439
+ if (-not (Test-Path -LiteralPath $root)) { return }
440
+ Assert-Root
441
+ $lockHandle = Enter-KitLock 50 100
442
+ try {
443
+ $stage = Join-Path $root ('.staging.' + $operationId)
444
+ if (Test-Path -LiteralPath $stage) { Remove-KitStage $stage }
445
+ } finally { Exit-KitLock $lockHandle }
446
+ }
447
+ function Install-Kit {
448
+ if (-not (Test-Path -LiteralPath $root)) { $null = [IO.Directory]::CreateDirectory($root) }
449
+ $null = Assert-Item $root $true
450
+ if ($onWindows) { Assert-WindowsPrivate $root $true } else {
451
+ Assert-PosixMode $root
452
+ & chmod 700 -- $root
453
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not protect cache root' }
454
+ }
455
+ Assert-Root
456
+ $lockHandle = $null; $stage = $null
457
+ try {
458
+ $lockHandle = Enter-KitLock 120 1000
459
+ try { Assert-Kit $final; return } catch { }
460
+ if (Test-Path -LiteralPath $final) {
461
+ $null = Assert-Item $final $true
462
+ if ($onWindows) { Assert-WindowsPrivate $final $true } else {
463
+ Assert-PosixMode $final
464
+ }
465
+ }
466
+ $stageId = if ($operationId) { $operationId } else { [Guid]::NewGuid().ToString('D') }
467
+ $candidate = Join-Path $root ('.staging.' + $stageId)
468
+ if (Test-Path -LiteralPath $candidate) { Stop-Stack 'operation staging path already exists' }
469
+ $stage = $candidate
470
+ $null = [IO.Directory]::CreateDirectory($stage)
471
+ if ($onWindows) { Assert-WindowsPrivate $stage $true } else { & chmod 700 -- $stage }
472
+ [Console]::Error.WriteLine("stack.ps1: downloading localmodels $StackVersion")
473
+ $archive = Join-Path $stage 'kit.tgz'
474
+ Resolve-Region
475
+ if ($env:LOCALMODELS_KIT_BASE_URL) {
476
+ Get-Archive "$($env:LOCALMODELS_KIT_BASE_URL.TrimEnd('/'))/$target.tgz" $archive
477
+ } else {
478
+ try { Get-Archive (Get-NpmUrl $script:region) $archive } catch [Net.WebException], [IO.IOException] {
479
+ if (Test-Path -LiteralPath $archive) { [IO.File]::Delete($archive) }
480
+ $script:region = if ($script:region -eq 'cn') { 'global' } else { 'cn' }
481
+ Get-Archive (Get-NpmUrl $script:region) $archive
482
+ }
483
+ }
484
+ if ((Get-Item -LiteralPath $archive).Length -ne [long]$pin.bytes -or (Get-Digest $archive) -cne $kit) { Stop-Stack 'archive SHA256 mismatch or size mismatch' }
485
+ $members = @(& tar -tvzf $archive 2>$null)
486
+ if ($LASTEXITCODE -ne 0 -or $members.Count -gt [int]$pin.entries) { Stop-Stack 'invalid kit archive' }
487
+ $sizeColumn = if ($onWindows -or $IsMacOS) { 4 } else { 2 }
488
+ $expandedSize = 0L
489
+ foreach ($member in $members) {
490
+ if ($member -notmatch '^[-d]') { Stop-Stack 'linked or special archive member' }
491
+ $fields = @($member -split '\s+')
492
+ $size = 0L
493
+ if (-not [long]::TryParse($fields[$sizeColumn], [ref]$size) -or $size -lt 0) { Stop-Stack 'invalid archive size' }
494
+ $expandedSize += $size
495
+ if ($expandedSize -gt 1073741824) { Stop-Stack 'kit archive exceeds extracted size limit' }
496
+ }
497
+ $names = @(& tar -tzf $archive 2>$null)
498
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'invalid kit archive' }
499
+ foreach ($name in $names) {
500
+ $relative = $name.TrimEnd('/')
501
+ Assert-Relative $relative
502
+ if (-not $relative.StartsWith('package/')) { Stop-Stack 'invalid npm package prefix' }
503
+ }
504
+ $unpacked = Join-Path $stage 'kit'
505
+ $null = [IO.Directory]::CreateDirectory($unpacked)
506
+ if ((Invoke-Native 'tar' @('-xzf', $archive, '--strip-components=1', '-C', $unpacked)) -ne 0) { Stop-Stack 'could not unpack kit' }
507
+ if (-not $onWindows) {
508
+ & chmod 755 -- (Join-Path $unpacked $pin.python) (Join-Path $unpacked 'kit.py')
509
+ if ($LASTEXITCODE -ne 0) { Stop-Stack 'could not restore executables' }
510
+ }
511
+ Set-KitMode $unpacked
512
+ Assert-Kit $unpacked
513
+ if (Test-Path -LiteralPath $final) {
514
+ if (-not $onWindows) { & chmod 700 -- $final }
515
+ [IO.Directory]::Move($final, (Join-Path $stage 'invalid'))
516
+ }
517
+ if (-not $onWindows) { & chmod 700 -- $unpacked }
518
+ [IO.Directory]::Move($unpacked, $final)
519
+ if (-not $onWindows) { & chmod 500 -- $final }
520
+ } finally {
521
+ if ($stage -and (Test-Path -LiteralPath $stage)) {
522
+ try { Remove-KitStage $stage $true } catch { }
523
+ }
524
+ if ($lockHandle) { Exit-KitLock $lockHandle }
525
+ }
526
+ }
527
+
528
+ try {
529
+ if (-not $StackVersion) { Stop-Stack 'this is the unpinned template; use a released launcher' }
530
+ $arguments = @($args)
531
+ $script:region = if ($env:SIMPLEX_REGION) { $env:SIMPLEX_REGION } else { 'auto' }
532
+ if ($arguments.Count -gt 0 -and $arguments[0] -in @('-Region', '--region')) {
533
+ if ($arguments.Count -lt 2) { Stop-Stack 'Region requires auto, global or cn' }
534
+ $script:region = $arguments[1]
535
+ $arguments = @($arguments | Select-Object -Skip 2)
536
+ }
537
+ if ($script:region -cnotin @('auto', 'global', 'cn')) { Stop-Stack 'Region requires auto, global or cn' }
538
+ $bootstrap = $arguments.Count -gt 0 -and $arguments[0] -ceq '--bootstrap-json'
539
+ $cleanup = $arguments.Count -gt 0 -and $arguments[0] -ceq '--bootstrap-cleanup'
540
+ $installedOnly = $false
541
+ $operationId = $null
542
+ if ($bootstrap -or $cleanup) {
543
+ if ($bootstrap -and $arguments.Count -eq 2 -and $arguments[1] -ceq '--installed-only') { $installedOnly = $true }
544
+ elseif ($arguments.Count -eq 3 -and $arguments[1] -ceq '--operation-id' -and
545
+ $arguments[2] -cmatch '\A[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}\z') {
546
+ $operationId = $arguments[2]
547
+ } elseif (-not ($bootstrap -and $arguments.Count -eq 1)) { Stop-Stack 'invalid bootstrap control arguments' }
548
+ } elseif (@($arguments | Where-Object { $_ -match '^--(bootstrap-json|bootstrap-cleanup|installed-only|operation-id)(=|$)' }).Count) {
549
+ Stop-Stack 'invalid bootstrap control arguments'
550
+ }
551
+ $onWindows = $PSVersionTable.PSVersion.Major -lt 6 -or $IsWindows
552
+ if ($onWindows) {
553
+ if ($installedOnly) { Stop-Stack 'installed-only validation is unsupported on Windows' }
554
+ $arch = if ($env:PROCESSOR_ARCHITEW6432) { $env:PROCESSOR_ARCHITEW6432 } else { $env:PROCESSOR_ARCHITECTURE }
555
+ $target = @{ AMD64 = 'win32-x64' }[$arch]
556
+ $cacheBase = $env:LOCALAPPDATA
557
+ } else {
558
+ $os = if ($IsMacOS) { 'darwin' } else { 'linux' }
559
+ $machine = (& uname -m).Trim()
560
+ $target = @{ x86_64 = "$os-x64"; amd64 = "$os-x64"; arm64 = "$os-arm64"; aarch64 = "$os-arm64" }[$machine]
561
+ $cacheBase = if ($env:XDG_CACHE_HOME) { $env:XDG_CACHE_HOME } else { Join-Path $HOME '.cache' }
562
+ $script:Owner = (& id -u).Trim()
563
+ }
564
+ if (-not $target -or -not $StackKits.ContainsKey($target)) { Stop-Stack 'release has no kit for this platform' }
565
+ $kit = $StackKits[$target]; $pin = $StackBootstrap[$target]
566
+ $root = if ($env:LOCALMODELS_KIT_HOME) { $env:LOCALMODELS_KIT_HOME } else { Join-Path $cacheBase 'simplex-localmodels/kits' }
567
+ $root = [IO.Path]::GetFullPath($root)
568
+ $final = Join-Path $root $kit
569
+ if ($cleanup) { Remove-OperationStage; exit 0 }
570
+ $valid = $false
571
+ try { Assert-Root; Assert-Kit $final; $valid = $true } catch { }
572
+ if (-not $valid) {
573
+ if ($installedOnly) { Stop-Stack 'installed kit validation failed' }
574
+ Install-Kit
575
+ }
576
+ $env:SIMPLEX_REGION = $script:region
577
+ if ($bootstrap) {
578
+ $exitCode = Invoke-Native (Join-Path $final $pin.python) @('-I', '-B', (Join-Path $final 'kit.py'), 'describe', $StackVersion, $target, $StackSourceCommit, $kit, $pin.manifest, $pin.python, $pin.modules)
579
+ } else {
580
+ if ($arguments.Count -eq 0) { $arguments = @('--help') }
581
+ $exitCode = Invoke-Native (Join-Path $final $pin.python) (@('-I', '-B', (Join-Path $final 'kit.py'), 'run', 'stack') + $arguments)
582
+ }
583
+ exit $exitCode
584
+ } catch {
585
+ $message = if ($_.Exception.Data['StackError']) { $_.Exception.Message } else { 'bootstrap validation or preparation failed' }
586
+ [Console]::Error.WriteLine("stack.ps1: $message")
587
+ exit 2
588
+ }
package/stack.sh ADDED
@@ -0,0 +1,336 @@
1
+ #!/bin/sh
2
+ set -eu
3
+
4
+ # >>> release pin
5
+ STACK_VERSION='0.1.0.dev28'
6
+ STACK_SOURCE_COMMIT='3e948cdda88604db5ec65f477afac627bf69db2c'
7
+ STACK_NPM_VERSION='0.1.0-dev.28.build.git3e948cdda88604db5ec65f477afac627bf69db2c'
8
+ STACK_KIT_darwin_arm64='f9e113ce48586d4076837647ddcf74b7e17a885e46026078a651ff9417ebe53d'
9
+ STACK_KIT_darwin_x64='22d0eec6661ccddd71bb55f4bc060ef557ac610471e37ae1b157244f49c184c9'
10
+ STACK_KIT_linux_arm64='ad0d8d1814213bb7654f192e8a7a2231c33d6fc66c264b36c47ea38573cac00a'
11
+ STACK_KIT_linux_x64='91227931e395cc4a5d383d3d1df7ee26d7df60ac47102d025c3b7ea938e859d1'
12
+ # <<< release pin
13
+
14
+ die() {
15
+ printf 'stack.sh: %s\n' "$1" >&2
16
+ exit 2
17
+ }
18
+
19
+ [ -n "${STACK_VERSION}" ] || die 'this is the unpinned template; use a released launcher'
20
+ region=${SIMPLEX_REGION:-auto}
21
+ if [ "${1:-}" = --region ]; then
22
+ [ "$#" -ge 2 ] || die '--region requires auto, global or cn'
23
+ region=$2
24
+ shift 2
25
+ fi
26
+ case "$region" in auto | global | cn) ;; *) die '--region requires auto, global or cn' ;; esac
27
+ bootstrap=false
28
+ installed_only=false
29
+ if [ "${1:-}" = --bootstrap-json ]; then
30
+ bootstrap=true
31
+ shift
32
+ if [ "${1:-}" = --installed-only ]; then installed_only=true; shift; fi
33
+ [ "$#" -eq 0 ] || die 'usage: --bootstrap-json [--installed-only]'
34
+ elif [ "${1:-}" = --installed-only ]; then
35
+ die '--installed-only requires --bootstrap-json'
36
+ fi
37
+
38
+ case "$(uname -s)-$(uname -m)" in
39
+ Linux-x86_64 | Linux-amd64) target=linux-x64; kit=${STACK_KIT_linux_x64} ;;
40
+ Linux-aarch64 | Linux-arm64) target=linux-arm64; kit=${STACK_KIT_linux_arm64} ;;
41
+ Darwin-arm64) target=darwin-arm64; kit=${STACK_KIT_darwin_arm64} ;;
42
+ Darwin-x86_64) target=darwin-x64; kit=${STACK_KIT_darwin_x64} ;;
43
+ *) die 'no localmodels kit for this platform; on Windows use stack.ps1' ;;
44
+ esac
45
+ [ -n "${kit}" ] || die "release ${STACK_VERSION} has no ${target} kit"
46
+ manifest='' inventory='' bytes='' files='' entries='' python='' modules=''
47
+ # >>> bootstrap targets
48
+ case "$target" in
49
+ darwin-arm64) manifest='e1f3fa3771acc33661515c1d977f5fb43017ed9d2c9defe7f31ea16acc031394'; inventory='c3c67cef554d1a0561ff613e8caa920b80144305dc373465cf36e7733d7f5dfb'; bytes='40712386'; files='1865'; entries='2029'; python='python/bin/python3.12'; modules='modules' ;;
50
+ darwin-x64) manifest='cb8e632424cf8ffe3d15c9fd1d2df1f3415c6e114ebba7cdb2139c6a8f1986d7'; inventory='0a942e3afa4e4825ae10fe6051a3df327294545c084ad88c774d940c92dc2ecc'; bytes='40193280'; files='1862'; entries='2025'; python='python/bin/python3.12'; modules='modules' ;;
51
+ linux-arm64) manifest='04efa34d89f5e851c2aae73c24e2e90c571774e5b059432cbeb1fb7676e5d784'; inventory='59e9f7d91dee72006b36402b951ca9a0189d14d8846019e009d1f2461e6559d9'; bytes='55658788'; files='4736'; entries='4943'; python='python/bin/python3.12'; modules='modules' ;;
52
+ linux-x64) manifest='58b7a5e8befec1f5a884821e45221d52c5914a261178a0efa6b2cb71e4e629a0'; inventory='1033dae80313953edca873f32816e3ee7846da6ba0269edd54904f6eba2c1faa'; bytes='68864113'; files='4736'; entries='4943'; python='python/bin/python3.12'; modules='modules' ;;
53
+ *) die 'release lacks bootstrap pins' ;;
54
+ esac
55
+ # <<< bootstrap targets
56
+ [ -n "$inventory" ] || die 'release lacks bootstrap pins'
57
+
58
+ root=${LOCALMODELS_KIT_HOME:-${XDG_CACHE_HOME:-${HOME}/.cache}/simplex-localmodels/kits}
59
+ case "$root" in /*) ;; *) root=$PWD/$root ;; esac
60
+ while [ "$root" != / ] && [ "${root%/}" != "$root" ]; do root=${root%/}; done
61
+ final=$root/$kit
62
+ owner=$(id -u)
63
+ staging=''
64
+ lock=''
65
+ child=''
66
+
67
+ cleanup() {
68
+ if [ -n "$child" ]; then kill "$child" 2>/dev/null || :; wait "$child" 2>/dev/null || :; fi
69
+ if [ -n "$staging" ]; then
70
+ find "$staging" -type d -user "$owner" -exec chmod 0700 {} + 2>/dev/null || :
71
+ rm -rf "$staging" 2>/dev/null || printf 'stack.sh: staging cleanup failed\n' >&2
72
+ fi
73
+ if [ -n "$lock" ]; then rmdir "$lock" 2>/dev/null || :; fi
74
+ }
75
+ trap cleanup EXIT
76
+ trap 'exit 130' INT
77
+ trap 'exit 143' TERM
78
+
79
+ sha256_of() (
80
+ if [ "$1" != - ]; then exec < "$1"; fi
81
+ if command -v sha256sum >/dev/null 2>&1; then
82
+ sha256sum | cut -d ' ' -f 1
83
+ elif command -v shasum >/dev/null 2>&1; then
84
+ shasum -a 256 | cut -d ' ' -f 1
85
+ else
86
+ die 'sha256sum or shasum is required'
87
+ fi
88
+ )
89
+
90
+ private_root() {
91
+ [ -d "$root" ] && [ ! -L "$root" ] &&
92
+ [ -z "$(find "$root" -prune \( ! -user "$owner" -o -perm -0040 -o -perm -0020 -o -perm -0010 -o -perm -0004 -o -perm -0002 -o -perm -0001 \) -print)" ] || return 1
93
+ ancestor=$(dirname "$root")
94
+ while :; do
95
+ [ ! -L "$ancestor" ] || return 1
96
+ bad_parent=$(find "$ancestor" -prune \( \( ! -user "$owner" ! -user 0 \) -o \( \( -perm -0020 -o -perm -0002 \) ! -perm -1000 \) \) -print) || return 1
97
+ [ -z "$bad_parent" ] || return 1
98
+ [ "$ancestor" != / ] || break
99
+ ancestor=$(dirname "$ancestor")
100
+ done
101
+ }
102
+
103
+ verify_tree() (
104
+ directory=$1
105
+ [ -d "$directory" ] && [ ! -L "$directory" ] || exit 1
106
+ bad=$(find "$directory" \( ! -user "$owner" -o ! \( -type d -o -type f \) -o \( -type d ! -perm 0500 \) -o \( -type f ! \( -perm 0400 -o -perm 0500 \) \) -o \( -type f -links +1 \) \) -print) || exit 1
107
+ [ -z "$bad" ] || exit 1
108
+ [ -x "$directory/$python" ] || exit 1
109
+ [ "$(find "$directory" -type f | wc -l | tr -d ' ')" = "$files" ] || exit 1
110
+ [ "$(find "$directory" | wc -l | tr -d ' ')" = "$entries" ] || exit 1
111
+ [ "$(sha256_of "$directory/bootstrap.sha256")" = "$inventory" ] || exit 1
112
+ [ "$(sha256_of "$directory/tool.json")" = "$manifest" ] || exit 1
113
+ [ "$(cat "$directory/bootstrap.identity")" = "$target $STACK_SOURCE_COMMIT $STACK_VERSION $python $modules" ] || exit 1
114
+ platform=${target%-*}
115
+ arch=${target##*-}
116
+ prefix="{\"schemaVersion\":1,\"platform\":\"$platform\",\"arch\":\"$arch\",\"sourceCommit\":\"$STACK_SOURCE_COMMIT\",\"python\":\"$python\",\"moduleDir\":\"$modules\""
117
+ reconstructed=$(LC_ALL=C awk -v prefix="$prefix" '
118
+ BEGIN {printf "%s,\"assets\":{", prefix}
119
+ $2!="tool.json" && $2!="bootstrap.identity" {
120
+ if(n++) printf ","
121
+ printf "\"%s\":\"%s\"", $2, $1
122
+ }
123
+ END {printf "}}"}
124
+ ' "$directory/bootstrap.sha256" | sha256_of -)
125
+ [ "$reconstructed" = "$manifest" ] || exit 1
126
+ cd "$directory"
127
+ if command -v sha256sum >/dev/null 2>&1; then
128
+ sha256sum -c bootstrap.sha256 >/dev/null 2>&1
129
+ else
130
+ shasum -a 256 -c bootstrap.sha256 >/dev/null 2>&1
131
+ fi
132
+ ) 2>/dev/null
133
+
134
+ curl_protocol() {
135
+ case "$1" in
136
+ http://127.0.0.1:* | http://localhost:*)
137
+ if [ -n "${SIMPLEX_NPM_REGISTRY:-}${LOCALMODELS_KIT_BASE_URL:-}" ]; then
138
+ printf '=http,https'
139
+ return
140
+ fi ;;
141
+ esac
142
+ printf '=https'
143
+ }
144
+
145
+ select_downloader() {
146
+ if command -v curl >/dev/null 2>&1; then
147
+ downloader=curl
148
+ return
149
+ fi
150
+ downloader=wget
151
+ wget_requirement='curl or GNU Wget is required; BusyBox wget cannot restrict redirects to HTTPS (on Alpine: apk add curl)'
152
+ wget_version=$(wget --version 2>/dev/null) || wget_version=''
153
+ case "$wget_version" in *'GNU Wget'*) ;; *) die "$wget_requirement" ;; esac
154
+ wget_help=$(wget --help 2>/dev/null) || wget_help=''
155
+ for wget_option in --https-only --max-redirect --server-response --ca-certificate; do
156
+ case "$wget_help" in *"$wget_option"*) ;; *) die "$wget_requirement" ;; esac
157
+ done
158
+ }
159
+
160
+ wget_secure() {
161
+ wget_url=$1 wget_output=$2 wget_timeout=$3 wget_range=${4:-}
162
+ wget_hops=0 wget_child=''
163
+ wget_headers=$(mktemp "$staging/.wget-headers.XXXXXX") || return 1
164
+ trap 'if [ -n "$wget_child" ]; then kill "$wget_child" 2>/dev/null || :; wait "$wget_child" 2>/dev/null || :; fi; rm -f "$wget_headers"' EXIT
165
+ trap 'exit 143' TERM
166
+ trap 'exit 130' INT
167
+ while :; do
168
+ case "$wget_url" in
169
+ https://*) ;;
170
+ http://127.0.0.1:* | http://localhost:*) [ "$wget_hops" -eq 0 ] && [ "$(curl_protocol "$wget_url")" = '=http,https' ] || return 1 ;;
171
+ *) return 1 ;;
172
+ esac
173
+ wget_authority=${wget_url#*://}; wget_authority=${wget_authority%%/*}
174
+ case "$wget_authority" in *@*) return 1 ;; esac
175
+ set -- --https-only --max-redirect=0 --server-response --tries=1 --timeout="$wget_timeout" -O "$wget_output"
176
+ if [ -n "${SSL_CERT_FILE:-}" ]; then set -- --ca-certificate "$SSL_CERT_FILE" "$@"; fi
177
+ if [ "$wget_range" = range ]; then set -- --header='Range: bytes=0-262143' "$@"; fi
178
+ wget "$@" "$wget_url" 2>"$wget_headers" &
179
+ wget_child=$! wget_status=0
180
+ wait "$wget_child" || wget_status=$?
181
+ wget_child=''
182
+ [ "$wget_status" -ne 0 ] || return 0
183
+ wget_code=$(LC_ALL=C awk '/^[[:space:]]*HTTP\// {code=$2} END {print code}' "$wget_headers")
184
+ case "$wget_code" in 301 | 302 | 303 | 307 | 308) ;; *) return "$wget_status" ;; esac
185
+ [ "$wget_hops" -lt 5 ] || return 1
186
+ wget_location=$(LC_ALL=C awk 'tolower($1)=="location:" {sub(/^[^:]*:[[:space:]]*/, ""); sub(/\r$/, ""); target=$0} END {print target}' "$wget_headers")
187
+ case "$wget_location" in
188
+ https://*) wget_url=$wget_location ;;
189
+ //*) wget_url=https:$wget_location ;;
190
+ /*) wget_url=$(printf '%s' "$wget_url" | cut -d / -f 1-3)$wget_location ;;
191
+ *://* | '') return 1 ;;
192
+ *) wget_url=${wget_url%/*}/$wget_location ;;
193
+ esac
194
+ wget_hops=$((wget_hops + 1))
195
+ done
196
+ }
197
+
198
+ fetch() {
199
+ if [ "$downloader" = curl ]; then
200
+ (ulimit -f "$(( (bytes + 511) / 512 ))"; exec curl -q -fsSL --proto "$(curl_protocol "$1")" --proto-redir '=https' --max-redirs 5 --retry 2 --connect-timeout 15 --max-time 300 --max-filesize "$bytes" -o "$2" "$1") 2>/dev/null &
201
+ else
202
+ (ulimit -f "$(( (bytes + 511) / 512 ))"; wget_secure "$1" "$2" 300) 2>/dev/null &
203
+ fi
204
+ child=$!
205
+ status=0
206
+ wait "$child" || status=$?
207
+ child=''
208
+ return "$status"
209
+ }
210
+
211
+ npm_url() {
212
+ case "$1" in
213
+ cn) base=https://registry.npmmirror.com ;;
214
+ global) base=https://registry.npmjs.org ;;
215
+ esac
216
+ base=${SIMPLEX_NPM_REGISTRY:-$base}
217
+ name=localmodels-$target
218
+ printf '%s/@simplexai/%s/-/%s-%s.tgz\n' "${base%/}" "$name" "$name" "$STACK_NPM_VERSION"
219
+ }
220
+
221
+ probe() (
222
+ if [ "$downloader" = curl ]; then
223
+ url=$(npm_url "$1")
224
+ curl -q -fsSL --proto "$(curl_protocol "$url")" --proto-redir '=https' --max-redirs 5 --range 0-262143 --max-time 5 -o /dev/null "$url" 2>/dev/null || exit 0
225
+ else
226
+ wget_secure "$(npm_url "$1")" /dev/null 5 range 2>/dev/null || exit 0
227
+ fi
228
+ if mkdir "$staging/winner" 2>/dev/null; then printf '%s\n' "$1" > "$staging/winner/region"; fi
229
+ )
230
+
231
+ resolve_region() {
232
+ if [ "$region" = auto ] && [ -n "${LOCALMODELS_KIT_BASE_URL:-}" ]; then region=global; fi
233
+ if [ "$region" = auto ]; then
234
+ probe global & global_probe=$!
235
+ probe cn & cn_probe=$!
236
+ (sleep 5; kill "$global_probe" "$cn_probe" 2>/dev/null || :) & probe_timer=$!
237
+ wait "$global_probe" || :
238
+ wait "$cn_probe" || :
239
+ kill "$probe_timer" 2>/dev/null || :
240
+ wait "$probe_timer" 2>/dev/null || :
241
+ region=global
242
+ if [ -f "$staging/winner/region" ]; then region=$(cat "$staging/winner/region"); fi
243
+ fi
244
+ printf 'stack.sh: using npm mirror %s (%s)\n' "$(npm_url "$region" | cut -d / -f 3)" "$region" >&2
245
+ }
246
+
247
+ install_kit() {
248
+ umask 077
249
+ if [ ! -e "$root" ] && [ ! -L "$root" ]; then
250
+ mkdir -p "$root" 2>/dev/null || die 'could not create cache root'
251
+ fi
252
+ # shellcheck disable=SC2015 # die exits, so "A && B || die" is a guard
253
+ [ -d "$root" ] && [ ! -L "$root" ] &&
254
+ [ -z "$(find "$root" -prune ! -user "$owner" -print)" ] || die 'cache root must be an owned directory'
255
+ chmod 0700 "$root" 2>/dev/null || die 'could not protect cache root'
256
+ private_root || die 'cache root must be a private owned directory'
257
+ attempts=0
258
+ while ! mkdir "$root/.lock-$kit" 2>/dev/null; do
259
+ attempts=$((attempts + 1))
260
+ [ "$attempts" -lt 120 ] || die 'cache lock timeout; retry after the other bootstrap exits'
261
+ sleep 1 &
262
+ child=$!
263
+ wait "$child"
264
+ child=''
265
+ done
266
+ lock=$root/.lock-$kit
267
+ if verify_tree "$final"; then return; fi
268
+ if [ -e "$final" ] || [ -L "$final" ]; then
269
+ # shellcheck disable=SC2015 # die exits, so "A && B || die" is a guard
270
+ [ -d "$final" ] && [ ! -L "$final" ] || die 'cache kit must be an owned directory'
271
+ [ -z "$(find "$final" -prune ! -user "$owner" -print)" ] || die 'cache kit must be an owned directory'
272
+ fi
273
+ staging=$(mktemp -d "$root/.staging.XXXXXX") || die 'could not create staging directory'
274
+ select_downloader
275
+ printf 'stack.sh: downloading localmodels %s\n' "$STACK_VERSION" >&2
276
+ resolve_region
277
+ if [ -n "${LOCALMODELS_KIT_BASE_URL:-}" ]; then
278
+ fetch "${LOCALMODELS_KIT_BASE_URL%/}/${target}.tgz" "$staging/kit.tgz" || die 'download failed'
279
+ elif ! fetch "$(npm_url "$region")" "$staging/kit.tgz"; then
280
+ if [ "$region" = cn ]; then region=global; else region=cn; fi
281
+ fetch "$(npm_url "$region")" "$staging/kit.tgz" || die 'download failed from both npm registries'
282
+ fi
283
+ # shellcheck disable=SC2015 # die exits, so "A && B || die" is a guard
284
+ [ "$(wc -c < "$staging/kit.tgz" | tr -d ' ')" = "$bytes" ] &&
285
+ [ "$(sha256_of "$staging/kit.tgz")" = "$kit" ] || die 'archive SHA256 mismatch or size mismatch'
286
+ tar -tzf "$staging/kit.tgz" > "$staging/names" 2>/dev/null || die 'invalid kit archive'
287
+ LC_ALL=C awk '
288
+ !/^package\// {exit 1}
289
+ /[^A-Za-z0-9_.+\/-]/ {exit 1}
290
+ /^\// {exit 1}
291
+ {sub(/\/$/, ""); n=split($0,p,"/"); for(i=1;i<=n;i++)
292
+ if(p[i]=="" || p[i]=="." || p[i]==".." || p[i]~/\.$/ ||
293
+ tolower(p[i])~/^(con|prn|aux|nul|com[1-9]|lpt[1-9])(\.|$)/) exit 1}
294
+ ' "$staging/names" || die 'unsafe kit archive path'
295
+ tar -tvzf "$staging/kit.tgz" > "$staging/members" 2>/dev/null || die 'invalid kit archive'
296
+ LC_ALL=C awk 'substr($0,1,1)!="-" && substr($0,1,1)!="d" {exit 1}' "$staging/members" || die 'linked or special kit archive member'
297
+ size_column=3
298
+ [ "$(uname -s)" != Darwin ] || size_column=5
299
+ LC_ALL=C awk -v column="$size_column" '
300
+ $column !~ /^[0-9]+$/ {exit 1}
301
+ {size+=$column; if(size>1073741824) exit 1}
302
+ ' "$staging/members" || die 'kit archive exceeds extracted size limit'
303
+ mkdir "$staging/kit" 2>/dev/null || die 'could not create kit directory'
304
+ tar -xzf "$staging/kit.tgz" --strip-components=1 -C "$staging/kit" 2>/dev/null &
305
+ child=$!
306
+ wait "$child" || die 'could not unpack kit'
307
+ child=''
308
+ chmod 0755 "$staging/kit/$python" "$staging/kit/kit.py" 2>/dev/null || die 'could not restore executables'
309
+ find "$staging/kit" -type f -exec chmod a-w,go-rwx {} + 2>/dev/null || die 'could not seal kit files'
310
+ find "$staging/kit" -type d -exec chmod 0500 {} + 2>/dev/null || die 'could not seal kit directories'
311
+ verify_tree "$staging/kit" || die 'downloaded kit failed verification'
312
+ if [ -e "$final" ]; then
313
+ chmod u+w "$final" 2>/dev/null || die 'could not reopen damaged kit'
314
+ mv "$final" "$staging/invalid" 2>/dev/null || die 'could not quarantine damaged kit'
315
+ fi
316
+ chmod 0700 "$staging/kit" 2>/dev/null || die 'could not prepare kit activation'
317
+ mv "$staging/kit" "$final" 2>/dev/null || die 'could not activate kit'
318
+ chmod 0500 "$final" 2>/dev/null || die 'could not seal activated kit'
319
+ }
320
+
321
+ if ! private_root || ! verify_tree "$final"; then
322
+ "$installed_only" && die 'installed kit validation failed'
323
+ install_kit
324
+ fi
325
+ cleanup
326
+ staging=''
327
+ lock=''
328
+ SIMPLEX_REGION=$region
329
+ export SIMPLEX_REGION
330
+ final=$(cd "$final" && pwd -P) || die 'could not resolve verified kit location'
331
+ if "$bootstrap"; then
332
+ exec "$final/$python" -I -B "$final/kit.py" describe "$STACK_VERSION" "$target" \
333
+ "$STACK_SOURCE_COMMIT" "$kit" "$manifest" "$python" "$modules"
334
+ fi
335
+ [ "$#" -gt 0 ] || set -- --help
336
+ exec "$final/$python" -I -B "$final/kit.py" run stack "$@"
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.