@wenathlan/saddle 2.0.151 → 2.0.152

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.
Files changed (293) hide show
  1. package/README.md +3 -3
  2. package/biome.jsonc +12 -5
  3. package/dist/App.d.ts +3 -0
  4. package/dist/App.js +50 -0
  5. package/dist/Sol/Sol.d.ts +3 -0
  6. package/dist/Sol/Sol.js +46 -0
  7. package/dist/Sol/agentbrowser/agentbrowser.d.ts +9 -0
  8. package/dist/Sol/agentbrowser/agentbrowser.js +121 -0
  9. package/dist/Sol/architecture/architecture.d.ts +9 -0
  10. package/dist/Sol/architecture/architecture.js +93 -0
  11. package/dist/Sol/compute/compute.d.ts +9 -0
  12. package/dist/Sol/compute/compute.js +22 -0
  13. package/dist/Sol/console/console.d.ts +15 -0
  14. package/dist/Sol/console/console.js +292 -0
  15. package/dist/Sol/dashboard/dashboard.d.ts +10 -0
  16. package/dist/Sol/dashboard/dashboard.js +181 -0
  17. package/dist/Sol/docs/docs.d.ts +9 -0
  18. package/dist/Sol/docs/docs.js +28 -0
  19. package/dist/Sol/home/home.d.ts +35 -0
  20. package/dist/Sol/home/home.js +239 -0
  21. package/dist/Sol/integrations/integrations.d.ts +9 -0
  22. package/dist/Sol/integrations/integrations.js +83 -0
  23. package/dist/Sol/intro/intro.d.ts +7 -0
  24. package/dist/Sol/intro/intro.js +102 -0
  25. package/dist/Sol/login/login.d.ts +16 -0
  26. package/dist/Sol/login/login.js +49 -0
  27. package/dist/Sol/notfound/notfound.d.ts +9 -0
  28. package/dist/Sol/notfound/notfound.js +15 -0
  29. package/dist/Sol/onboarding/onboarding.d.ts +25 -0
  30. package/dist/Sol/onboarding/onboarding.js +60 -0
  31. package/dist/Sol/playground/playground.d.ts +9 -0
  32. package/dist/Sol/playground/playground.js +127 -0
  33. package/dist/Sol/register/register.d.ts +16 -0
  34. package/dist/Sol/register/register.js +193 -0
  35. package/dist/Sol/shell/Shell.d.ts +124 -0
  36. package/dist/Sol/shell/Shell.js +335 -0
  37. package/dist/Sol/wingets/ComputeBootladder.d.ts +6 -0
  38. package/dist/Sol/wingets/ComputeBootladder.js +71 -0
  39. package/dist/Sol/wingets/ComputeModalitycatalog.d.ts +7 -0
  40. package/dist/Sol/wingets/ComputeModalitycatalog.js +71 -0
  41. package/dist/Sol/wingets/ConsoleApibadge.d.ts +7 -0
  42. package/dist/Sol/wingets/ConsoleApibadge.js +5 -0
  43. package/dist/Sol/wingets/ConsoleEdgelaw.d.ts +6 -0
  44. package/dist/Sol/wingets/ConsoleEdgelaw.js +36 -0
  45. package/dist/Sol/wingets/ConsoleEventstimeline.d.ts +19 -0
  46. package/dist/Sol/wingets/ConsoleEventstimeline.js +12 -0
  47. package/dist/Sol/wingets/ConsoleSnapshotcontrols.d.ts +8 -0
  48. package/dist/Sol/wingets/ConsoleSnapshotcontrols.js +8 -0
  49. package/dist/Sol/wingets/ConsoleSpecpanel.d.ts +21 -0
  50. package/dist/Sol/wingets/ConsoleSpecpanel.js +75 -0
  51. package/dist/Sol/wingets/ConsoleTerminal.d.ts +22 -0
  52. package/dist/Sol/wingets/ConsoleTerminal.js +77 -0
  53. package/dist/Sol/wingets/DashboardAccountpanel.d.ts +18 -0
  54. package/dist/Sol/wingets/DashboardAccountpanel.js +65 -0
  55. package/dist/Sol/wingets/DashboardAdminpanel.d.ts +6 -0
  56. package/dist/Sol/wingets/DashboardAdminpanel.js +90 -0
  57. package/dist/Sol/wingets/DashboardAuditlog.d.ts +8 -0
  58. package/dist/Sol/wingets/DashboardAuditlog.js +50 -0
  59. package/dist/Sol/wingets/DashboardCreatesandboxform.d.ts +10 -0
  60. package/dist/Sol/wingets/DashboardCreatesandboxform.js +71 -0
  61. package/dist/Sol/wingets/DashboardEventstimeline.d.ts +8 -0
  62. package/dist/Sol/wingets/DashboardEventstimeline.js +83 -0
  63. package/dist/Sol/wingets/DashboardGlobalsandboxestable.d.ts +8 -0
  64. package/dist/Sol/wingets/DashboardGlobalsandboxestable.js +57 -0
  65. package/dist/Sol/wingets/DashboardMeshnodestable.d.ts +8 -0
  66. package/dist/Sol/wingets/DashboardMeshnodestable.js +103 -0
  67. package/dist/Sol/wingets/DashboardSandboxlist.d.ts +10 -0
  68. package/dist/Sol/wingets/DashboardSandboxlist.js +163 -0
  69. package/dist/Sol/wingets/DashboardUserstable.d.ts +8 -0
  70. package/dist/Sol/wingets/DashboardUserstable.js +78 -0
  71. package/dist/Sol/wingets/HomeEntrygate.d.ts +23 -0
  72. package/dist/Sol/wingets/HomeEntrygate.js +73 -0
  73. package/dist/Sol/wingets/HomeMetricstrip.d.ts +9 -0
  74. package/dist/Sol/wingets/HomeMetricstrip.js +4 -0
  75. package/dist/Sol/wingets/HomeRuntimediagram.d.ts +1 -0
  76. package/dist/Sol/wingets/HomeRuntimediagram.js +22 -0
  77. package/dist/Sol/wingets/LoginForm.d.ts +19 -0
  78. package/dist/Sol/wingets/LoginForm.js +141 -0
  79. package/dist/Sol/wingets/RegisterPasswordconfirm.d.ts +13 -0
  80. package/dist/Sol/wingets/RegisterPasswordconfirm.js +16 -0
  81. package/dist/Sol/wingets/RegisterPasswordmeter.d.ts +19 -0
  82. package/dist/Sol/wingets/RegisterPasswordmeter.js +65 -0
  83. package/dist/Sol/wingets/RegisterUsernamerules.d.ts +11 -0
  84. package/dist/Sol/wingets/RegisterUsernamerules.js +16 -0
  85. package/dist/Sol/wingets/index.d.ts +39 -0
  86. package/dist/Sol/wingets/index.js +39 -0
  87. package/dist/alternatives.d.ts +508 -0
  88. package/dist/alternatives.js +950 -0
  89. package/dist/api.d.ts +154 -0
  90. package/dist/api.js +174 -0
  91. package/dist/auth.d.ts +268 -0
  92. package/dist/auth.js +476 -0
  93. package/dist/automation.d.ts +425 -0
  94. package/dist/automation.js +566 -0
  95. package/dist/browser.d.ts +561 -0
  96. package/dist/browser.js +1058 -0
  97. package/dist/capacitor.config.d.ts +45 -0
  98. package/dist/capacitor.config.js +48 -0
  99. package/dist/catalog.d.ts +43 -0
  100. package/dist/catalog.js +44 -0
  101. package/dist/cli.d.ts +16 -0
  102. package/dist/cli.js +168 -0
  103. package/dist/communication.d.ts +181 -0
  104. package/dist/communication.js +376 -0
  105. package/dist/compute.d.ts +1160 -0
  106. package/dist/compute.js +2272 -0
  107. package/dist/dashboardlib.d.ts +58 -0
  108. package/dist/dashboardlib.js +161 -0
  109. package/dist/db.d.ts +569 -0
  110. package/dist/db.js +1080 -0
  111. package/dist/distribution.d.ts +528 -0
  112. package/dist/distribution.js +810 -0
  113. package/dist/drizzle.config.d.ts +20 -0
  114. package/dist/drizzle.config.js +18 -0
  115. package/dist/edgelandes.d.ts +70 -0
  116. package/dist/edgelandes.host.d.ts +26 -0
  117. package/dist/edgelandes.host.js +1089 -0
  118. package/dist/edgelandes.inproc.d.ts +27 -0
  119. package/dist/edgelandes.inproc.js +952 -0
  120. package/dist/edgelandes.js +1133 -0
  121. package/dist/edgelandes.ninety.d.ts +31 -0
  122. package/dist/edgelandes.ninety.js +174 -0
  123. package/dist/edgelandes.virtual.d.ts +25 -0
  124. package/dist/edgelandes.virtual.js +629 -0
  125. package/dist/edgeplane.d.ts +114 -0
  126. package/dist/edgeplane.js +305 -0
  127. package/dist/examplelocaljob.d.ts +1 -0
  128. package/{docs/example-localjob.ts → dist/examplelocaljob.js} +0 -1
  129. package/dist/execution.d.ts +529 -0
  130. package/dist/execution.js +760 -0
  131. package/dist/family.d.ts +16 -0
  132. package/dist/family.js +42 -0
  133. package/dist/familyurl.d.ts +21 -0
  134. package/dist/familyurl.js +24 -0
  135. package/dist/format.d.ts +18 -0
  136. package/dist/format.js +59 -0
  137. package/dist/foundation.d.ts +172 -0
  138. package/dist/foundation.js +312 -0
  139. package/dist/index.d.ts +735 -0
  140. package/dist/index.js +1020 -0
  141. package/dist/integration.d.ts +235 -0
  142. package/dist/integration.js +228 -0
  143. package/dist/intelligence.d.ts +93 -0
  144. package/dist/intelligence.js +144 -0
  145. package/dist/isolation.d.ts +128 -0
  146. package/dist/isolation.js +95 -0
  147. package/dist/labels.d.ts +665 -0
  148. package/dist/labels.js +751 -0
  149. package/dist/localauth.d.ts +127 -0
  150. package/dist/localauth.js +413 -0
  151. package/dist/media.d.ts +343 -0
  152. package/dist/media.js +985 -0
  153. package/dist/mesh.d.ts +177 -0
  154. package/dist/mesh.js +468 -0
  155. package/dist/modalityregistry.d.ts +109 -0
  156. package/dist/modalityregistry.js +1386 -0
  157. package/dist/modes.d.ts +358 -0
  158. package/dist/modes.js +133 -0
  159. package/dist/onboardingstate.d.ts +79 -0
  160. package/dist/onboardingstate.js +282 -0
  161. package/dist/orchestrator.d.ts +1338 -0
  162. package/dist/orchestrator.js +4310 -0
  163. package/dist/packager/targetcli.js +4 -0
  164. package/dist/paths.d.ts +2 -0
  165. package/dist/paths.js +14 -0
  166. package/dist/performance.d.ts +415 -0
  167. package/dist/performance.js +826 -0
  168. package/dist/quantum.d.ts +809 -0
  169. package/dist/quantum.js +2072 -0
  170. package/dist/render.d.ts +380 -0
  171. package/dist/render.js +1072 -0
  172. package/dist/runall.d.ts +2 -0
  173. package/dist/runall.js +39 -0
  174. package/dist/runtimeprobe.d.ts +4 -0
  175. package/dist/runtimeprobe.js +8 -0
  176. package/dist/sandbox.d.ts +355 -0
  177. package/dist/sandbox.js +1721 -0
  178. package/dist/sandboxenvprofile.d.ts +29 -0
  179. package/dist/sandboxenvprofile.js +89 -0
  180. package/dist/sandboxprofile.d.ts +137 -0
  181. package/dist/sandboxprofile.js +241 -0
  182. package/dist/sandboxqueue.d.ts +108 -0
  183. package/dist/sandboxqueue.js +201 -0
  184. package/dist/sandboxquota.d.ts +121 -0
  185. package/dist/sandboxquota.js +236 -0
  186. package/dist/scheduler.d.ts +595 -0
  187. package/dist/scheduler.js +1258 -0
  188. package/dist/scriptlanes.d.ts +182 -0
  189. package/dist/scriptlanes.js +660 -0
  190. package/dist/scripts.buildengine.d.ts +1 -0
  191. package/dist/scripts.buildengine.js +39 -0
  192. package/dist/scripts.securitygate.d.ts +1 -0
  193. package/dist/scripts.securitygate.js +68 -0
  194. package/dist/scripts.validateflatnative.d.ts +1 -0
  195. package/dist/scripts.validateflatnative.js +72 -0
  196. package/dist/security.d.ts +500 -0
  197. package/dist/security.js +1169 -0
  198. package/dist/seedcatalog.d.ts +13 -0
  199. package/dist/seedcatalog.js +46 -0
  200. package/dist/server.d.ts +20 -0
  201. package/dist/server.js +53 -0
  202. package/dist/speccatalog.d.ts +31 -0
  203. package/dist/speccatalog.js +163 -0
  204. package/dist/tiers.d.ts +1001 -0
  205. package/dist/tiers.js +2169 -0
  206. package/dist/utils.d.ts +2 -0
  207. package/dist/utils.js +5 -0
  208. package/dist/vaulthandoff.d.ts +141 -0
  209. package/dist/vaulthandoff.js +444 -0
  210. package/dist/virtual.d.ts +640 -0
  211. package/dist/virtual.js +1284 -0
  212. package/dist/virtualcpu.d.ts +315 -0
  213. package/dist/virtualcpu.js +1180 -0
  214. package/dist/virtualgpu.d.ts +344 -0
  215. package/dist/virtualgpu.js +1000 -0
  216. package/dist/virtualization.d.ts +606 -0
  217. package/dist/virtualization.js +1186 -0
  218. package/dist/virtualmemory.d.ts +817 -0
  219. package/dist/virtualmemory.js +1795 -0
  220. package/dist/vite.config.d.ts +3 -0
  221. package/dist/vite.config.js +43 -0
  222. package/dist/vitest.config.d.ts +2 -0
  223. package/dist/vitest.config.js +25 -0
  224. package/dist/webserver.d.ts +166 -0
  225. package/dist/webserver.js +2226 -0
  226. package/dist/workloadlaw.d.ts +133 -0
  227. package/dist/workloadlaw.js +193 -0
  228. package/docs/artifactavailability.md +2 -2
  229. package/docs/{containerplatforms-2.1.0.md → containerplatforms.2.1.0.md} +1 -1
  230. package/docs/{documentmap-1.8.12.md → documentmap.1.8.12.md} +1 -1
  231. package/docs/ninety.modalities.md +108 -0
  232. package/docs/planning.ninety.md +391 -0
  233. package/docs/{releasenotes-1.8.13.md → releasenotes.1.8.13.md} +2 -2
  234. package/docs/{releasenotes-1.8.14.md → releasenotes.1.8.14.md} +1 -1
  235. package/docs/{repo-synthesis-1.8.16.md → repo.synthesis.1.8.16.md} +1 -1
  236. package/docs/{todo-1.8.15.md → todo.1.8.15.md} +1 -1
  237. package/docs/{todo-1.8.16.md → todo.1.8.16.md} +5 -5
  238. package/docs/todo.md +2 -2
  239. package/docs/workloadlaw.md +104 -0
  240. package/index.ts +1 -1
  241. package/manifest.json +1 -1
  242. package/modalityregistry.ts +1371 -34
  243. package/orchestrator.ts +1 -1
  244. package/package.json +73 -33
  245. package/docs/CONVERSA.txt +0 -1523
  246. package/docs/architecture-1.8.19.png +0 -0
  247. package/docs/assets/architecture.svg +0 -45
  248. package/docs/assets/saddle-browser-trace.webp +0 -0
  249. package/docs/assets/saddle-hero-bridge.webp +0 -0
  250. package/docs/assets/saddle-mark.webp +0 -0
  251. package/docs/assets/saddle-runtime-map.webp +0 -0
  252. package/docs/assets/saddlemark.svg +0 -13
  253. package/docs/saddle.archive.1.8.17.tar.gz.gpg +0 -0
  254. /package/docs/{architecture-1.8.19.md → architecture.1.8.19.md} +0 -0
  255. /package/docs/{architecture-1.8.19.mmd → architecture.1.8.19.mmd} +0 -0
  256. /package/docs/{artifactresearch-1.8.9.md → artifactresearch.1.8.9.md} +0 -0
  257. /package/docs/{batches-1816-1820.md → batches.1816.to.1820.md} +0 -0
  258. /package/docs/{brancharchive-2026-08-18.md → brancharchive.2026.08.18.md} +0 -0
  259. /package/docs/{containerplatforms-1.8.17.md → containerplatforms.1.8.17.md} +0 -0
  260. /package/docs/{e2ugh-engine.md → e2ugh.engine.md} +0 -0
  261. /package/docs/{flatbuildresearch-1.8.11.md → flatbuildresearch.1.8.11.md} +0 -0
  262. /package/docs/{licenseaudit-1.8.12.md → licenseaudit.1.8.12.md} +0 -0
  263. /package/docs/{mobileconversionresearch-1.8.10.md → mobileconversionresearch.1.8.10.md} +0 -0
  264. /package/docs/{native-wrappers.md → native.wrappers.md} +0 -0
  265. /package/docs/{nativeidentityresearch-1.8.11.md → nativeidentityresearch.1.8.11.md} +0 -0
  266. /package/docs/{releasenotes-1.8.12.md → releasenotes.1.8.12.md} +0 -0
  267. /package/docs/{releasenotes-1.8.15.md → releasenotes.1.8.15.md} +0 -0
  268. /package/docs/{releasenotes-1.8.16.md → releasenotes.1.8.16.md} +0 -0
  269. /package/docs/{releasenotes-1.8.17.md → releasenotes.1.8.17.md} +0 -0
  270. /package/docs/{releasenotes-1.8.18.md → releasenotes.1.8.18.md} +0 -0
  271. /package/docs/{releasenotes-1.8.19.md → releasenotes.1.8.19.md} +0 -0
  272. /package/docs/{reorganization-1.8.8.md → reorganization.1.8.8.md} +0 -0
  273. /package/docs/{reorganization-1.8.9.md → reorganization.1.8.9.md} +0 -0
  274. /package/docs/{repo-analysis-1.8.21.json → repo.analysis.1.8.21.json} +0 -0
  275. /package/docs/{repo-candidates-1.8.21.jsonl → repo.candidates.1.8.21.jsonl} +0 -0
  276. /package/docs/{repo-research-1.8.16.md → repo.research.1.8.16.md} +0 -0
  277. /package/docs/{repo-research-1.8.21.md → repo.research.1.8.21.md} +0 -0
  278. /package/docs/{repo-selected-1.8.21.json → repo.selected.1.8.21.json} +0 -0
  279. /package/docs/{repo-synthesis-1.8.21.md → repo.synthesis.1.8.21.md} +0 -0
  280. /package/docs/{research-1.8.18-isolation.md → research.1.8.18.isolation.md} +0 -0
  281. /package/docs/{research-1.8.19-virtual-browser.md → research.1.8.19.virtual.browser.md} +0 -0
  282. /package/docs/{scanningresearch-1.8.11.md → scanningresearch.1.8.11.md} +0 -0
  283. /package/docs/{securityaudit-1.8.7.md → securityaudit.1.8.7.md} +0 -0
  284. /package/docs/{securityreferences-1.8.11.md → securityreferences.1.8.11.md} +0 -0
  285. /package/docs/{securityresearch-1.8.11.md → securityresearch.1.8.11.md} +0 -0
  286. /package/docs/{signingresearch-1.8.12.md → signingresearch.1.8.12.md} +0 -0
  287. /package/docs/{tauri-glib-risk-1.8.11.md → tauri.glib.risk.1.8.11.md} +0 -0
  288. /package/docs/{todo-1.8.18.md → todo.1.8.18.md} +0 -0
  289. /package/docs/{todo-1.8.19.md → todo.1.8.19.md} +0 -0
  290. /package/docs/{toolchainresearch-1.8.9.md → toolchainresearch.1.8.9.md} +0 -0
  291. /package/docs/{web-duplicatecleanup.md → web.duplicatecleanup.md} +0 -0
  292. /package/docs/{web-ideas.md → web.ideas.md} +0 -0
  293. /package/docs/{workflowimprovements-2026-08-19.md → workflowimprovements.2026.08.19.md} +0 -0
@@ -0,0 +1,201 @@
1
+ /**
2
+ * sandboxqueue.ts — the sandbox lifecycle queue of saddle.
3
+ *
4
+ * one sandbox walks a single linear machine: creating -> starting ->
5
+ * running -> stopping -> stopped, with the supervision states cancelled,
6
+ * failed and timedout closing the paths out. every intermediate state
7
+ * carries a per-state time budget; the health-check tick reaps entries
8
+ * whose budget expired (the ttl reaper pattern of webserver.ts and the
9
+ * pool reaper of opensandbox). transitions are validated by a pure
10
+ * transition table so both callers and tests exercise the same guard.
11
+ *
12
+ * the module is deliberately pure: no timers of its own, no dom, no node
13
+ * builtins, an injectable clock. hosts drive it — webserver.ts ramps
14
+ * the api lifecycle with a timeout, the ttl reaper sweeps every minute,
15
+ * and orchestrator.ts owns the fifteen state vm plane machine; this
16
+ * module is the queue-level complement those surfaces can delegate to
17
+ * (wire point documented in the webserver statemachine context: the
18
+ * api states map as created->creating, expired->timedout,
19
+ * destroyed->stopped).
20
+ *
21
+ * studied competitors shaping the contract: daytonaio/daytona (sandbox
22
+ * states creating/starting/started/stopping/stopped/error with the
23
+ * -ing intermediates) and opensandbox-group/opensandbox (Creating ->
24
+ * Running -> Deleting -> Deleted with 409 on state conflicting
25
+ * operations and a pool acquisition that fails fast with Retry-After).
26
+ */
27
+ /** phases that end the queue entry (no further transition is legal). */
28
+ export const SANDBOXTERMINALPHASES = [
29
+ 'stopped',
30
+ 'cancelled',
31
+ 'failed',
32
+ 'timedout',
33
+ ];
34
+ /** the legal transitions of the single lifecycle machine. */
35
+ const sandboxtransitions = {
36
+ creating: ['starting', 'cancelled', 'failed', 'timedout'],
37
+ starting: ['running', 'cancelled', 'failed', 'timedout'],
38
+ running: ['stopping', 'cancelled', 'failed', 'timedout'],
39
+ stopping: ['stopped', 'failed', 'timedout'],
40
+ stopped: [],
41
+ cancelled: [],
42
+ failed: [],
43
+ timedout: [],
44
+ };
45
+ /** the typed queue failure, traceable to the entry and both phases. */
46
+ export class sandboxqueueerror extends Error {
47
+ /** machine readable failure code. */
48
+ code;
49
+ /** the entry the failure belongs to. */
50
+ id;
51
+ /** the phase the entry was in (or from), when known. */
52
+ from;
53
+ /** the requested target phase, when known. */
54
+ to;
55
+ constructor(code, id, from, to, message) {
56
+ super(message ??
57
+ `sandbox queue ${code} for ${id}${from === null ? '' : ` (from ${from})`}${to === null ? '' : ` to ${to}`}`);
58
+ this.name = 'sandboxqueueerror';
59
+ this.code = code;
60
+ this.id = id;
61
+ this.from = from;
62
+ this.to = to;
63
+ }
64
+ }
65
+ /* ------------------------------------------------------------------ */
66
+ /* pure guards */
67
+ /* ------------------------------------------------------------------ */
68
+ /** checks whether a phase change is legal on the single machine. */
69
+ export function cansandboxstep(from, to) {
70
+ return sandboxtransitions[from].includes(to);
71
+ }
72
+ /** reports whether a phase is terminal. */
73
+ export function issandboxterminal(phase) {
74
+ return SANDBOXTERMINALPHASES.includes(phase);
75
+ }
76
+ /**
77
+ * creates the lifecycle queue. the queue validates every transition,
78
+ * stamps deadlines from the budgets and reaps expired entries on the
79
+ * health-check tick; the host decides when to tick.
80
+ *
81
+ * @param options the budgets and event ring capacity.
82
+ * @returns the queue facade (add, step, cancel, healthcheck, status,
83
+ * list, events, size).
84
+ */
85
+ export function createsandboxqueue(options) {
86
+ const budgets = options?.budgets ?? {
87
+ creating: 125,
88
+ starting: 125,
89
+ stopping: 30000,
90
+ };
91
+ const eventcap = options?.eventcap ?? 100;
92
+ const entries = new Map();
93
+ const ring = [];
94
+ /** stamps one event into the bounded ring. */
95
+ function record(id, from, to, at, reason) {
96
+ ring.push({ id, from, to, at, reason });
97
+ if (ring.length > eventcap) {
98
+ ring.splice(0, ring.length - eventcap);
99
+ }
100
+ }
101
+ /** resolves the deadline of a phase from the budget table. */
102
+ function deadlinefor(state, now) {
103
+ const budget = budgets[state];
104
+ return typeof budget === 'number' && budget > 0 ? now + budget : null;
105
+ }
106
+ function statusOf(entry, now) {
107
+ return {
108
+ id: entry.id,
109
+ state: entry.state,
110
+ enteredat: entry.enteredat,
111
+ deadline: entry.deadline,
112
+ age: Math.max(0, now - entry.enteredat),
113
+ terminal: issandboxterminal(entry.state),
114
+ };
115
+ }
116
+ return {
117
+ add(id, now = Date.now()) {
118
+ if (entries.has(id)) {
119
+ throw new sandboxqueueerror('duplicate-sandbox', id, null, null);
120
+ }
121
+ const entry = {
122
+ id,
123
+ state: 'creating',
124
+ enteredat: now,
125
+ deadline: deadlinefor('creating', now),
126
+ };
127
+ entries.set(id, entry);
128
+ record(id, 'creating', 'creating', now, 'requested');
129
+ return entry;
130
+ },
131
+ step(id, to, now = Date.now()) {
132
+ const entry = entries.get(id);
133
+ if (entry === undefined) {
134
+ throw new sandboxqueueerror('unknown-sandbox', id, null, to);
135
+ }
136
+ if (!cansandboxstep(entry.state, to)) {
137
+ if (issandboxterminal(entry.state)) {
138
+ throw new sandboxqueueerror('terminal-phase', id, entry.state, to, `sandbox ${id} is terminal (${entry.state}); ${to} is unreachable`);
139
+ }
140
+ throw new sandboxqueueerror('invalid-transition', id, entry.state, to, `sandbox ${id} cannot transition ${entry.state} -> ${to}`);
141
+ }
142
+ const from = entry.state;
143
+ entry.state = to;
144
+ entry.enteredat = now;
145
+ entry.deadline = deadlinefor(to, now);
146
+ record(id, from, to, now, 'requested');
147
+ return entry;
148
+ },
149
+ cancel(id, now = Date.now()) {
150
+ const entry = entries.get(id);
151
+ if (entry === undefined) {
152
+ throw new sandboxqueueerror('unknown-sandbox', id, null, 'cancelled');
153
+ }
154
+ const from = entry.state;
155
+ if (!cansandboxstep(from, 'cancelled')) {
156
+ if (issandboxterminal(from)) {
157
+ throw new sandboxqueueerror('terminal-phase', id, from, 'cancelled', `sandbox ${id} is terminal (${from}); cancellation is unreachable`);
158
+ }
159
+ throw new sandboxqueueerror('invalid-transition', id, from, 'cancelled', `sandbox ${id} cannot be cancelled from ${from}`);
160
+ }
161
+ entry.state = 'cancelled';
162
+ entry.enteredat = now;
163
+ entry.deadline = null;
164
+ record(id, from, 'cancelled', now, 'cancel');
165
+ return entry;
166
+ },
167
+ healthcheck(now = Date.now()) {
168
+ const reaped = [];
169
+ for (const entry of entries.values()) {
170
+ if (entry.deadline === null || entry.deadline > now || issandboxterminal(entry.state)) {
171
+ continue;
172
+ }
173
+ const from = entry.state;
174
+ entry.state = 'timedout';
175
+ entry.enteredat = now;
176
+ entry.deadline = null;
177
+ record(entry.id, from, 'timedout', now, 'timeout');
178
+ reaped.push(entry.id);
179
+ }
180
+ return reaped;
181
+ },
182
+ status(id, now = Date.now()) {
183
+ const entry = entries.get(id);
184
+ if (entry === undefined) {
185
+ throw new sandboxqueueerror('unknown-sandbox', id, null, null);
186
+ }
187
+ return statusOf(entry, now);
188
+ },
189
+ list(now = Date.now()) {
190
+ return [...entries.values()]
191
+ .map((entry) => statusOf(entry, now))
192
+ .sort((a, b) => (a.id < b.id ? -1 : 1));
193
+ },
194
+ events() {
195
+ return [...ring];
196
+ },
197
+ size() {
198
+ return entries.size;
199
+ },
200
+ };
201
+ }
@@ -0,0 +1,121 @@
1
+ /**
2
+ * sandboxquota.ts — the per-account resource quota ledger of saddle.
3
+ *
4
+ * every account holds counters of the resources its live sandboxes
5
+ * allocate (sandbox count, vcpus, ramgb, vgpus, diskgb) against a
6
+ * limits table. the limits are a plain parameter — the operator's plan
7
+ * table comes from config or the database and never lives in code; an
8
+ * absent limit means unlimited, mirroring the SADDLE_MAX_SANDBOXES
9
+ * convention of webserver.ts (0 or unset means unlimited).
10
+ *
11
+ * the ledger keeps reservations and releases in balance with a simple
12
+ * reconciliation: the host projects the authoritative usage (for
13
+ * example from the sqlite sandboxes rows) and reconcile() overwrites
14
+ * the in-memory counters with it, so drift from admin deletes, restarts
15
+ * or crashes self-heals on the next create.
16
+ *
17
+ * the module is pure: zero imports, zero dom, zero node builtins, a
18
+ * Map per account, typed traceable errors (quotaexceedederror carries
19
+ * the resource, the request, the limit and the remaining headroom).
20
+ *
21
+ * competitor sources shaping the contract: daytonaio/daytona (tenant
22
+ * resource quota admission with per-sandbox cpu/memory/gpu quota
23
+ * fields and the gpu allocator acquire/release pair around create) and
24
+ * opensandbox-group/opensandbox (namespace ResourceQuota admission —
25
+ * the sandbox is not admitted when the quota is exhausted — and the
26
+ * pool acquisition that fails fast when capacity stays unavailable).
27
+ *
28
+ * wire point: webserver.ts createsandbox reconciles the owning
29
+ * account from store.listsandboxesbyuser rows and reserves the new
30
+ * sandbox; destroysandbox releases it. limits come from
31
+ * accountlimitsfromenv(process.env) and stay unlimited when unset.
32
+ */
33
+ /** the resource counters one account allocates through live sandboxes. */
34
+ export type accountusage = {
35
+ sandboxes: number;
36
+ vcpus: number;
37
+ ramgb: number;
38
+ vgpus: number;
39
+ diskgb: number;
40
+ };
41
+ /** the per-account limits; an absent or non-positive field is unlimited. */
42
+ export type accountlimits = {
43
+ readonly sandboxes?: number;
44
+ readonly vcpus?: number;
45
+ readonly ramgb?: number;
46
+ readonly vgpus?: number;
47
+ readonly diskgb?: number;
48
+ };
49
+ /** one resource wish (a partial usage delta). */
50
+ export type resourcewish = Partial<accountusage>;
51
+ /** failure codes carried by quotaerror. */
52
+ export type quotaerrorcode = 'invalid-wish' | 'quota-exceeded';
53
+ /** the typed quota failure, traceable to the account and the resource. */
54
+ export declare class quotaerror extends Error {
55
+ /** machine readable failure code. */
56
+ readonly code: quotaerrorcode;
57
+ /** the account the failure belongs to. */
58
+ readonly accountid: string;
59
+ /** the resource that failed (or 'wish' for malformed deltas). */
60
+ readonly resource: string;
61
+ /** the requested amount (null when the wish itself was malformed). */
62
+ readonly requested: number | null;
63
+ /** the active limit (null when unlimited). */
64
+ readonly limit: number | null;
65
+ /** the headroom the account had before the request. */
66
+ readonly available: number | null;
67
+ constructor(code: quotaerrorcode, accountid: string, resource: string, requested: number | null, limit: number | null, available: number | null, message?: string);
68
+ }
69
+ /** a read-only snapshot answered by usage(). */
70
+ export type accountquotasnapshot = {
71
+ readonly accountid: string;
72
+ readonly used: Readonly<accountusage>;
73
+ readonly remaining: Readonly<Record<keyof accountusage, number | null>>;
74
+ };
75
+ /**
76
+ * creates the per-account ledger with one limits table for every
77
+ * account (per-plan tables ride on the caller passing the plan row of
78
+ * the account).
79
+ *
80
+ * @param limits the limits table (absent fields are unlimited).
81
+ * @returns the ledger facade (admits, reserve, release, usage,
82
+ * reconcile, reset).
83
+ */
84
+ export declare function createaccountledger(limits: accountlimits): {
85
+ admits: (accountid: string, wish: resourcewish) => boolean;
86
+ reserve: (accountid: string, wish: resourcewish) => accountquotasnapshot;
87
+ release: (accountid: string, wish: resourcewish) => accountquotasnapshot;
88
+ usage: (accountid: string) => accountquotasnapshot;
89
+ reconcile: (accountid: string, observed: accountusage) => {
90
+ before: accountquotasnapshot;
91
+ after: accountquotasnapshot;
92
+ };
93
+ reset: (accountid?: string) => void;
94
+ };
95
+ /** the structural row shape the projection reads (SandboxRow of db.ts
96
+ * satisfies it; kept structural so the module stays import-free). */
97
+ export type sandboxrowlike = {
98
+ readonly vcpus: number | null;
99
+ readonly ramgb: number | null;
100
+ readonly state: string | null;
101
+ };
102
+ /**
103
+ * projects sandbox rows onto one account usage: every row that is not
104
+ * destroyed counts as a live allocation (the same semantics the
105
+ * SADDLE_MAX_SANDBOXES cap check of webserver.ts uses).
106
+ *
107
+ * @param rows the authoritative rows of one account.
108
+ * @returns the usage counters the rows allocate.
109
+ */
110
+ export declare function usagefromrows(rows: readonly sandboxrowlike[]): accountusage;
111
+ /**
112
+ * reads the per-account limits from the environment: SADDLE_MAX_SANDBOXES
113
+ * (the existing count cap of webserver.ts), SADDLE_ACCOUNT_MAX_VCPUS,
114
+ * SADDLE_ACCOUNT_MAX_RAM_GB, SADDLE_ACCOUNT_MAX_VGPUS and
115
+ * SADDLE_ACCOUNT_MAX_DISK_GB. unset, zero and negative values mean
116
+ * unlimited, so the default deployment enforces nothing.
117
+ *
118
+ * @param env the environment record (process.env shaped).
119
+ * @returns the limits table.
120
+ */
121
+ export declare function accountlimitsfromenv(env: Record<string, string | undefined>): accountlimits;
@@ -0,0 +1,236 @@
1
+ /**
2
+ * sandboxquota.ts — the per-account resource quota ledger of saddle.
3
+ *
4
+ * every account holds counters of the resources its live sandboxes
5
+ * allocate (sandbox count, vcpus, ramgb, vgpus, diskgb) against a
6
+ * limits table. the limits are a plain parameter — the operator's plan
7
+ * table comes from config or the database and never lives in code; an
8
+ * absent limit means unlimited, mirroring the SADDLE_MAX_SANDBOXES
9
+ * convention of webserver.ts (0 or unset means unlimited).
10
+ *
11
+ * the ledger keeps reservations and releases in balance with a simple
12
+ * reconciliation: the host projects the authoritative usage (for
13
+ * example from the sqlite sandboxes rows) and reconcile() overwrites
14
+ * the in-memory counters with it, so drift from admin deletes, restarts
15
+ * or crashes self-heals on the next create.
16
+ *
17
+ * the module is pure: zero imports, zero dom, zero node builtins, a
18
+ * Map per account, typed traceable errors (quotaexceedederror carries
19
+ * the resource, the request, the limit and the remaining headroom).
20
+ *
21
+ * competitor sources shaping the contract: daytonaio/daytona (tenant
22
+ * resource quota admission with per-sandbox cpu/memory/gpu quota
23
+ * fields and the gpu allocator acquire/release pair around create) and
24
+ * opensandbox-group/opensandbox (namespace ResourceQuota admission —
25
+ * the sandbox is not admitted when the quota is exhausted — and the
26
+ * pool acquisition that fails fast when capacity stays unavailable).
27
+ *
28
+ * wire point: webserver.ts createsandbox reconciles the owning
29
+ * account from store.listsandboxesbyuser rows and reserves the new
30
+ * sandbox; destroysandbox releases it. limits come from
31
+ * accountlimitsfromenv(process.env) and stay unlimited when unset.
32
+ */
33
+ /** the resource names tracked by the ledger. */
34
+ const RESOURCES = [
35
+ 'sandboxes',
36
+ 'vcpus',
37
+ 'ramgb',
38
+ 'vgpus',
39
+ 'diskgb',
40
+ ];
41
+ /** the typed quota failure, traceable to the account and the resource. */
42
+ export class quotaerror extends Error {
43
+ /** machine readable failure code. */
44
+ code;
45
+ /** the account the failure belongs to. */
46
+ accountid;
47
+ /** the resource that failed (or 'wish' for malformed deltas). */
48
+ resource;
49
+ /** the requested amount (null when the wish itself was malformed). */
50
+ requested;
51
+ /** the active limit (null when unlimited). */
52
+ limit;
53
+ /** the headroom the account had before the request. */
54
+ available;
55
+ constructor(code, accountid, resource, requested, limit, available, message) {
56
+ super(message ??
57
+ `account ${accountid} ${code} on ${resource}: requested ${requested ?? '?'} of ${limit ?? 'unlimited'} (${available ?? '?'} available)`);
58
+ this.name = 'quotaerror';
59
+ this.code = code;
60
+ this.accountid = accountid;
61
+ this.resource = resource;
62
+ this.requested = requested;
63
+ this.limit = limit;
64
+ this.available = available;
65
+ }
66
+ }
67
+ /* ------------------------------------------------------------------ */
68
+ /* helpers */
69
+ /* ------------------------------------------------------------------ */
70
+ /** validates one resource wish: every present field is a positive number. */
71
+ function sanitize(wish) {
72
+ const clean = {};
73
+ for (const resource of RESOURCES) {
74
+ const value = wish[resource];
75
+ if (value === undefined) {
76
+ continue;
77
+ }
78
+ if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) {
79
+ throw new quotaerror('invalid-wish', '', resource, typeof value === 'number' ? value : null, null, null, `resource ${resource} must be a positive finite number (received ${String(value)})`);
80
+ }
81
+ clean[resource] = value;
82
+ }
83
+ return clean;
84
+ }
85
+ /** headroom of one resource under its limit (null limit means Infinity). */
86
+ function headroom(limit, used) {
87
+ return typeof limit === 'number' && limit > 0 ? Math.max(0, limit - used) : null;
88
+ }
89
+ /**
90
+ * creates the per-account ledger with one limits table for every
91
+ * account (per-plan tables ride on the caller passing the plan row of
92
+ * the account).
93
+ *
94
+ * @param limits the limits table (absent fields are unlimited).
95
+ * @returns the ledger facade (admits, reserve, release, usage,
96
+ * reconcile, reset).
97
+ */
98
+ export function createaccountledger(limits) {
99
+ const books = new Map();
100
+ /** zeroes the counters of an unseen account. */
101
+ function blank(accountid) {
102
+ const existing = books.get(accountid);
103
+ if (existing !== undefined) {
104
+ return existing;
105
+ }
106
+ const fresh = { sandboxes: 0, vcpus: 0, ramgb: 0, vgpus: 0, diskgb: 0 };
107
+ books.set(accountid, fresh);
108
+ return fresh;
109
+ }
110
+ function snapshotOf(accountid, used) {
111
+ const remaining = {};
112
+ for (const resource of RESOURCES) {
113
+ remaining[resource] = headroom(limits[resource], used[resource]);
114
+ }
115
+ return { accountid, used: { ...used }, remaining };
116
+ }
117
+ /** the shared admit-or-throw walk used by admits and reserve. */
118
+ function check(accountid, wish) {
119
+ const clean = sanitize(wish);
120
+ const used = blank(accountid);
121
+ for (const resource of RESOURCES) {
122
+ const request = clean[resource];
123
+ if (request === undefined) {
124
+ continue;
125
+ }
126
+ const room = headroom(limits[resource], used[resource]);
127
+ if (room !== null && request > room) {
128
+ throw new quotaerror('quota-exceeded', accountid, resource, request, limits[resource] ?? null, room);
129
+ }
130
+ }
131
+ return clean;
132
+ }
133
+ return {
134
+ admits(accountid, wish) {
135
+ try {
136
+ check(accountid, wish);
137
+ return true;
138
+ }
139
+ catch (error) {
140
+ if (error instanceof quotaerror) {
141
+ return false;
142
+ }
143
+ throw error;
144
+ }
145
+ },
146
+ reserve(accountid, wish) {
147
+ const clean = check(accountid, wish);
148
+ const used = blank(accountid);
149
+ for (const resource of RESOURCES) {
150
+ const request = clean[resource];
151
+ if (request !== undefined) {
152
+ used[resource] += request;
153
+ }
154
+ }
155
+ return snapshotOf(accountid, used);
156
+ },
157
+ release(accountid, wish) {
158
+ const clean = sanitize(wish);
159
+ const used = blank(accountid);
160
+ for (const resource of RESOURCES) {
161
+ const request = clean[resource];
162
+ if (request !== undefined) {
163
+ used[resource] = Math.max(0, used[resource] - request);
164
+ }
165
+ }
166
+ return snapshotOf(accountid, used);
167
+ },
168
+ usage(accountid) {
169
+ return snapshotOf(accountid, blank(accountid));
170
+ },
171
+ reconcile(accountid, observed) {
172
+ const used = blank(accountid);
173
+ const before = snapshotOf(accountid, used);
174
+ for (const resource of RESOURCES) {
175
+ const value = observed[resource];
176
+ used[resource] =
177
+ typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0;
178
+ }
179
+ const after = snapshotOf(accountid, used);
180
+ return { before, after };
181
+ },
182
+ reset(accountid) {
183
+ if (accountid === undefined) {
184
+ books.clear();
185
+ return;
186
+ }
187
+ books.delete(accountid);
188
+ },
189
+ };
190
+ }
191
+ /**
192
+ * projects sandbox rows onto one account usage: every row that is not
193
+ * destroyed counts as a live allocation (the same semantics the
194
+ * SADDLE_MAX_SANDBOXES cap check of webserver.ts uses).
195
+ *
196
+ * @param rows the authoritative rows of one account.
197
+ * @returns the usage counters the rows allocate.
198
+ */
199
+ export function usagefromrows(rows) {
200
+ const usage = { sandboxes: 0, vcpus: 0, ramgb: 0, vgpus: 0, diskgb: 0 };
201
+ for (const row of rows) {
202
+ if (row.state === 'destroyed') {
203
+ continue;
204
+ }
205
+ usage.sandboxes += 1;
206
+ usage.vcpus += typeof row.vcpus === 'number' ? row.vcpus : 0;
207
+ usage.ramgb += typeof row.ramgb === 'number' ? row.ramgb : 0;
208
+ }
209
+ return usage;
210
+ }
211
+ /* ------------------------------------------------------------------ */
212
+ /* adapter: limits from the environment */
213
+ /* ------------------------------------------------------------------ */
214
+ /**
215
+ * reads the per-account limits from the environment: SADDLE_MAX_SANDBOXES
216
+ * (the existing count cap of webserver.ts), SADDLE_ACCOUNT_MAX_VCPUS,
217
+ * SADDLE_ACCOUNT_MAX_RAM_GB, SADDLE_ACCOUNT_MAX_VGPUS and
218
+ * SADDLE_ACCOUNT_MAX_DISK_GB. unset, zero and negative values mean
219
+ * unlimited, so the default deployment enforces nothing.
220
+ *
221
+ * @param env the environment record (process.env shaped).
222
+ * @returns the limits table.
223
+ */
224
+ export function accountlimitsfromenv(env) {
225
+ const positive = (name) => {
226
+ const parsed = Number(env[name]);
227
+ return Number.isFinite(parsed) && parsed > 0 ? Math.floor(parsed) : undefined;
228
+ };
229
+ return {
230
+ sandboxes: positive('SADDLE_MAX_SANDBOXES'),
231
+ vcpus: positive('SADDLE_ACCOUNT_MAX_VCPUS'),
232
+ ramgb: positive('SADDLE_ACCOUNT_MAX_RAM_GB'),
233
+ vgpus: positive('SADDLE_ACCOUNT_MAX_VGPUS'),
234
+ diskgb: positive('SADDLE_ACCOUNT_MAX_DISK_GB'),
235
+ };
236
+ }