@automaton-labs/aib 0.0.1

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 (291) hide show
  1. package/README.md +578 -0
  2. package/dist/bin/aib.js +2 -0
  3. package/dist/bin/cli.js +3 -0
  4. package/dist/client/http-client.js +1 -0
  5. package/dist/client/transport-options.js +1 -0
  6. package/dist/client/websocket-client.js +1 -0
  7. package/dist/commands/add-missing-imports-preview-cache.js +2 -0
  8. package/dist/commands/add-missing-imports.js +2 -0
  9. package/dist/commands/apply-module-plan.js +3 -0
  10. package/dist/commands/captured-input.js +2 -0
  11. package/dist/commands/config-command.js +3 -0
  12. package/dist/commands/diagnostics-command.js +3 -0
  13. package/dist/commands/doctor.js +1 -0
  14. package/dist/commands/entity-resolution.js +1 -0
  15. package/dist/commands/execution-command.js +1 -0
  16. package/dist/commands/feedback-command.js +3 -0
  17. package/dist/commands/find-importers.js +1 -0
  18. package/dist/commands/find-unused-imports.js +1 -0
  19. package/dist/commands/generate-docs-command.js +6 -0
  20. package/dist/commands/help-command.js +3 -0
  21. package/dist/commands/imports-command.js +1 -0
  22. package/dist/commands/init-skill-usage.js +3 -0
  23. package/dist/commands/init-workspace.js +3 -0
  24. package/dist/commands/inspect-cycles.js +5 -0
  25. package/dist/commands/inspect-format.js +18 -0
  26. package/dist/commands/inspect-graph.js +9 -0
  27. package/dist/commands/inspect-imports.js +2 -0
  28. package/dist/commands/inspect-symbol-query.js +1 -0
  29. package/dist/commands/inspect-tree.js +5 -0
  30. package/dist/commands/inspect.js +16 -0
  31. package/dist/commands/json-file-input.js +1 -0
  32. package/dist/commands/list-instances.js +1 -0
  33. package/dist/commands/module-plan-cache.js +2 -0
  34. package/dist/commands/module-plan-command.js +1 -0
  35. package/dist/commands/module-plan-timeout.js +1 -0
  36. package/dist/commands/move-command.js +1 -0
  37. package/dist/commands/move-preview-cache.js +2 -0
  38. package/dist/commands/move-to-file.js +2 -0
  39. package/dist/commands/mutation-execution-cache.js +3 -0
  40. package/dist/commands/normalize-imports.js +4 -0
  41. package/dist/commands/observability-command.js +3 -0
  42. package/dist/commands/ping.js +1 -0
  43. package/dist/commands/print-config.js +1 -0
  44. package/dist/commands/quick-read.js +11 -0
  45. package/dist/commands/refactor-batch-builder.js +1 -0
  46. package/dist/commands/refactor-batch-execution-cache.js +1 -0
  47. package/dist/commands/refactor-batch-preview-cache.js +2 -0
  48. package/dist/commands/refactor-batch.js +4 -0
  49. package/dist/commands/refactor-command.js +1 -0
  50. package/dist/commands/rename-command.js +1 -0
  51. package/dist/commands/rename-entities.js +4 -0
  52. package/dist/commands/rename-execution-cache.js +1 -0
  53. package/dist/commands/rename-input.js +1 -0
  54. package/dist/commands/rename-preview-cache.js +2 -0
  55. package/dist/commands/result-view.js +6 -0
  56. package/dist/commands/runtime-command.js +4 -0
  57. package/dist/commands/runtime-info.js +1 -0
  58. package/dist/commands/session-workspace.js +31 -0
  59. package/dist/commands/shared.js +1 -0
  60. package/dist/commands/sync-command.js +2 -0
  61. package/dist/commands/validate-module-plan.js +2 -0
  62. package/dist/commands/workspace-cache.js +1 -0
  63. package/dist/compatibility/policy.js +1 -0
  64. package/dist/config/auto-start-ide.js +1 -0
  65. package/dist/config/defaults.js +1 -0
  66. package/dist/config/env-vars.js +1 -0
  67. package/dist/config/glob-match.js +1 -0
  68. package/dist/config/import-rules.js +1 -0
  69. package/dist/config/local-config.js +3 -0
  70. package/dist/config/mutation-comments.js +1 -0
  71. package/dist/config/path-aliases.js +1 -0
  72. package/dist/config/path-display.js +1 -0
  73. package/dist/config/product-storage.js +1 -0
  74. package/dist/config/resolve-command-alias.js +1 -0
  75. package/dist/config/resolve.js +1 -0
  76. package/dist/config/workspace-root.js +3 -0
  77. package/dist/config/workspace-state.js +1 -0
  78. package/dist/content/content-bundle.js +1 -0
  79. package/dist/diagnostics/module-plan-timeline.js +2 -0
  80. package/dist/diagnostics/readiness-text.js +8 -0
  81. package/dist/discovery/registry.js +1 -0
  82. package/dist/discovery/select-instance.js +1 -0
  83. package/dist/dsl/aib-dsl.js +1 -0
  84. package/dist/help/bootstrap.md +924 -0
  85. package/dist/help/diagnostics/doctor.json +70 -0
  86. package/dist/help/docs/basics.md +14 -0
  87. package/dist/help/docs/dsl.md +25 -0
  88. package/dist/help/docs/help-format.md +12 -0
  89. package/dist/help/docs/imports.normalize.md +36 -0
  90. package/dist/help/docs/inspect.code.md +29 -0
  91. package/dist/help/docs/inspect.deps.md +32 -0
  92. package/dist/help/docs/inspect.duplicates.md +72 -0
  93. package/dist/help/docs/inspect.exports.md +34 -0
  94. package/dist/help/docs/inspect.file.md +32 -0
  95. package/dist/help/docs/inspect.graph.md +112 -0
  96. package/dist/help/docs/inspect.imports.md +15 -0
  97. package/dist/help/docs/inspect.md +71 -0
  98. package/dist/help/docs/inspect.members.md +24 -0
  99. package/dist/help/docs/inspect.tree.md +30 -0
  100. package/dist/help/docs/inspect.usages.md +48 -0
  101. package/dist/help/docs/modulePlan.md +51 -0
  102. package/dist/help/docs/move.md +37 -0
  103. package/dist/help/docs/mutation.md +47 -0
  104. package/dist/help/docs/patterns.md +178 -0
  105. package/dist/help/docs/prefs.md +40 -0
  106. package/dist/help/docs/qr.md +33 -0
  107. package/dist/help/docs/refactor.batch.md +44 -0
  108. package/dist/help/docs/rename.md +39 -0
  109. package/dist/help/docs/selectors.md +23 -0
  110. package/dist/help/docs/session.md +61 -0
  111. package/dist/help/docs/view.md +30 -0
  112. package/dist/help/dsl/bootstrap.md +924 -0
  113. package/dist/help/dsl/docs/basics.md +14 -0
  114. package/dist/help/dsl/docs/dsl.md +25 -0
  115. package/dist/help/dsl/docs/help-format.md +12 -0
  116. package/dist/help/dsl/docs/imports.normalize.md +36 -0
  117. package/dist/help/dsl/docs/inspect.code.md +29 -0
  118. package/dist/help/dsl/docs/inspect.deps.md +32 -0
  119. package/dist/help/dsl/docs/inspect.duplicates.md +72 -0
  120. package/dist/help/dsl/docs/inspect.exports.md +34 -0
  121. package/dist/help/dsl/docs/inspect.file.md +32 -0
  122. package/dist/help/dsl/docs/inspect.graph.md +112 -0
  123. package/dist/help/dsl/docs/inspect.imports.md +15 -0
  124. package/dist/help/dsl/docs/inspect.md +71 -0
  125. package/dist/help/dsl/docs/inspect.members.md +24 -0
  126. package/dist/help/dsl/docs/inspect.tree.md +30 -0
  127. package/dist/help/dsl/docs/inspect.usages.md +48 -0
  128. package/dist/help/dsl/docs/modulePlan.md +51 -0
  129. package/dist/help/dsl/docs/move.md +37 -0
  130. package/dist/help/dsl/docs/mutation.md +47 -0
  131. package/dist/help/dsl/docs/patterns.md +178 -0
  132. package/dist/help/dsl/docs/prefs.md +40 -0
  133. package/dist/help/dsl/docs/qr.md +33 -0
  134. package/dist/help/dsl/docs/refactor.batch.md +44 -0
  135. package/dist/help/dsl/docs/rename.md +39 -0
  136. package/dist/help/dsl/docs/selectors.md +23 -0
  137. package/dist/help/dsl/docs/session.md +61 -0
  138. package/dist/help/dsl/docs/view.md +30 -0
  139. package/dist/help/dsl/full.md +924 -0
  140. package/dist/help/dsl/snippets/agents.md +14 -0
  141. package/dist/help/dsl/topics/basics.md +12 -0
  142. package/dist/help/dsl/topics/dsl.md +23 -0
  143. package/dist/help/dsl/topics/help-format.md +10 -0
  144. package/dist/help/dsl/topics/imports.normalize.md +34 -0
  145. package/dist/help/dsl/topics/inspect.code.md +27 -0
  146. package/dist/help/dsl/topics/inspect.deps.md +30 -0
  147. package/dist/help/dsl/topics/inspect.duplicates.md +43 -0
  148. package/dist/help/dsl/topics/inspect.exports.md +32 -0
  149. package/dist/help/dsl/topics/inspect.file.md +30 -0
  150. package/dist/help/dsl/topics/inspect.graph.md +110 -0
  151. package/dist/help/dsl/topics/inspect.imports.md +13 -0
  152. package/dist/help/dsl/topics/inspect.md +69 -0
  153. package/dist/help/dsl/topics/inspect.members.md +22 -0
  154. package/dist/help/dsl/topics/inspect.tree.md +20 -0
  155. package/dist/help/dsl/topics/inspect.usages.md +46 -0
  156. package/dist/help/dsl/topics/modulePlan.md +38 -0
  157. package/dist/help/dsl/topics/move.md +27 -0
  158. package/dist/help/dsl/topics/mutation.md +45 -0
  159. package/dist/help/dsl/topics/patterns.md +176 -0
  160. package/dist/help/dsl/topics/prefs.md +38 -0
  161. package/dist/help/dsl/topics/qr.md +31 -0
  162. package/dist/help/dsl/topics/refactor.batch.md +33 -0
  163. package/dist/help/dsl/topics/rename.md +25 -0
  164. package/dist/help/dsl/topics/selectors.md +21 -0
  165. package/dist/help/dsl/topics/session.md +59 -0
  166. package/dist/help/dsl/topics/view.md +28 -0
  167. package/dist/help/full.md +924 -0
  168. package/dist/help/help-meta.json +113 -0
  169. package/dist/help/index.md +167 -0
  170. package/dist/help/json/bootstrap.md +1074 -0
  171. package/dist/help/json/docs/basics.md +15 -0
  172. package/dist/help/json/docs/dsl.md +25 -0
  173. package/dist/help/json/docs/help-format.md +12 -0
  174. package/dist/help/json/docs/imports.normalize.md +47 -0
  175. package/dist/help/json/docs/inspect.code.md +41 -0
  176. package/dist/help/json/docs/inspect.deps.md +46 -0
  177. package/dist/help/json/docs/inspect.duplicates.md +65 -0
  178. package/dist/help/json/docs/inspect.exports.md +40 -0
  179. package/dist/help/json/docs/inspect.file.md +38 -0
  180. package/dist/help/json/docs/inspect.graph.md +139 -0
  181. package/dist/help/json/docs/inspect.imports.md +15 -0
  182. package/dist/help/json/docs/inspect.md +87 -0
  183. package/dist/help/json/docs/inspect.members.md +32 -0
  184. package/dist/help/json/docs/inspect.tree.md +30 -0
  185. package/dist/help/json/docs/inspect.usages.md +61 -0
  186. package/dist/help/json/docs/modulePlan.md +70 -0
  187. package/dist/help/json/docs/move.md +53 -0
  188. package/dist/help/json/docs/mutation.md +62 -0
  189. package/dist/help/json/docs/patterns.md +178 -0
  190. package/dist/help/json/docs/prefs.md +40 -0
  191. package/dist/help/json/docs/qr.md +33 -0
  192. package/dist/help/json/docs/refactor.batch.md +72 -0
  193. package/dist/help/json/docs/rename.md +47 -0
  194. package/dist/help/json/docs/selectors.md +23 -0
  195. package/dist/help/json/docs/session.md +77 -0
  196. package/dist/help/json/docs/view.md +30 -0
  197. package/dist/help/json/full.md +1074 -0
  198. package/dist/help/json/snippets/agents.md +14 -0
  199. package/dist/help/json/topics/basics.md +13 -0
  200. package/dist/help/json/topics/dsl.md +23 -0
  201. package/dist/help/json/topics/help-format.md +10 -0
  202. package/dist/help/json/topics/imports.normalize.md +45 -0
  203. package/dist/help/json/topics/inspect.code.md +39 -0
  204. package/dist/help/json/topics/inspect.deps.md +44 -0
  205. package/dist/help/json/topics/inspect.duplicates.md +37 -0
  206. package/dist/help/json/topics/inspect.exports.md +38 -0
  207. package/dist/help/json/topics/inspect.file.md +36 -0
  208. package/dist/help/json/topics/inspect.graph.md +137 -0
  209. package/dist/help/json/topics/inspect.imports.md +13 -0
  210. package/dist/help/json/topics/inspect.md +85 -0
  211. package/dist/help/json/topics/inspect.members.md +30 -0
  212. package/dist/help/json/topics/inspect.tree.md +20 -0
  213. package/dist/help/json/topics/inspect.usages.md +59 -0
  214. package/dist/help/json/topics/modulePlan.md +57 -0
  215. package/dist/help/json/topics/move.md +43 -0
  216. package/dist/help/json/topics/mutation.md +60 -0
  217. package/dist/help/json/topics/patterns.md +176 -0
  218. package/dist/help/json/topics/prefs.md +38 -0
  219. package/dist/help/json/topics/qr.md +31 -0
  220. package/dist/help/json/topics/refactor.batch.md +61 -0
  221. package/dist/help/json/topics/rename.md +42 -0
  222. package/dist/help/json/topics/selectors.md +21 -0
  223. package/dist/help/json/topics/session.md +59 -0
  224. package/dist/help/json/topics/view.md +28 -0
  225. package/dist/help/snippets/agents.md +14 -0
  226. package/dist/help/topics/basics.md +12 -0
  227. package/dist/help/topics/dsl.md +23 -0
  228. package/dist/help/topics/help-format.md +10 -0
  229. package/dist/help/topics/imports.normalize.md +34 -0
  230. package/dist/help/topics/inspect.code.md +27 -0
  231. package/dist/help/topics/inspect.deps.md +30 -0
  232. package/dist/help/topics/inspect.duplicates.md +43 -0
  233. package/dist/help/topics/inspect.exports.md +32 -0
  234. package/dist/help/topics/inspect.file.md +30 -0
  235. package/dist/help/topics/inspect.graph.md +110 -0
  236. package/dist/help/topics/inspect.imports.md +13 -0
  237. package/dist/help/topics/inspect.md +69 -0
  238. package/dist/help/topics/inspect.members.md +22 -0
  239. package/dist/help/topics/inspect.tree.md +20 -0
  240. package/dist/help/topics/inspect.usages.md +46 -0
  241. package/dist/help/topics/modulePlan.md +38 -0
  242. package/dist/help/topics/move.md +27 -0
  243. package/dist/help/topics/mutation.md +45 -0
  244. package/dist/help/topics/patterns.md +176 -0
  245. package/dist/help/topics/prefs.md +38 -0
  246. package/dist/help/topics/qr.md +31 -0
  247. package/dist/help/topics/refactor.batch.md +33 -0
  248. package/dist/help/topics/rename.md +25 -0
  249. package/dist/help/topics/selectors.md +21 -0
  250. package/dist/help/topics/session.md +59 -0
  251. package/dist/help/topics/view.md +28 -0
  252. package/dist/ide-launch/common.cjs +162 -0
  253. package/dist/managed-host/extension-vsix-resolver.js +1 -0
  254. package/dist/managed-host/manage-serve-web-host.cjs +141 -0
  255. package/dist/managed-host/serve-web-autostart.js +1 -0
  256. package/dist/managed-host/serve-web-host.cjs +1790 -0
  257. package/dist/metrics/central-metrics.js +2 -0
  258. package/dist/observability/config.js +1 -0
  259. package/dist/observability/context.js +1 -0
  260. package/dist/observability/failure-snapshot.js +2 -0
  261. package/dist/observability/recent.js +2 -0
  262. package/dist/payloads/read-stdin-json.js +1 -0
  263. package/dist/runtime/bundled-node.js +1 -0
  264. package/dist/runtime/input-source.js +1 -0
  265. package/dist/runtime/managed-runtime-provisioning.js +1 -0
  266. package/dist/runtime/run-command.js +1 -0
  267. package/dist/selectors/parse-entities.js +1 -0
  268. package/dist/session/client.js +1 -0
  269. package/dist/session/paths.js +1 -0
  270. package/dist/session/server.js +6 -0
  271. package/dist/shared/agent-text.js +1 -0
  272. package/dist/shared/diagnostic-catalog.js +1 -0
  273. package/dist/shared/diagnostics.js +1 -0
  274. package/dist/shared/errors.js +28 -0
  275. package/dist/shared/event-loop.js +1 -0
  276. package/dist/shared/hints.js +1 -0
  277. package/dist/shared/metrics.js +1 -0
  278. package/dist/shared/operations.js +1 -0
  279. package/dist/shared/presentation.js +4 -0
  280. package/dist/shared/protocol.js +1 -0
  281. package/dist/shared/routes.js +1 -0
  282. package/dist/shared/stdout.js +2 -0
  283. package/dist/shared/types.js +1 -0
  284. package/dist/tracing/config.js +2 -0
  285. package/dist/tracing/trace.js +3 -0
  286. package/extension/vscode-refactor-bridge-extension.vsix +0 -0
  287. package/package.json +39 -0
  288. package/runtimes/launcher/win-x64/aib.exe +0 -0
  289. package/scripts/install-windows-launcher.cjs +58 -0
  290. package/scripts/postinstall.cjs +28 -0
  291. package/scripts/provision-runtime.cjs +299 -0
@@ -0,0 +1,1790 @@
1
+ #!/usr/bin/env node
2
+
3
+ const crypto = require("node:crypto");
4
+ const fs = require("node:fs");
5
+ const net = require("node:net");
6
+ const os = require("node:os");
7
+ const path = require("node:path");
8
+ const { spawn, spawnSync } = require("node:child_process");
9
+ const {
10
+ defaultFixtureDir,
11
+ defaultVsixPath,
12
+ prepareSpawnCommand,
13
+ readRegistryEntries,
14
+ resolveIdeCommand,
15
+ rootDir,
16
+ run,
17
+ sleep,
18
+ timestampForPath,
19
+ writeJson
20
+ } = require("../ide-launch/common.cjs");
21
+
22
+ const managedRootDir = path.join(os.tmpdir(), "aib", "managed-hosts");
23
+ const defaultRunsDir = path.join(rootDir, ".tmp", "managed-host-runs");
24
+
25
+ function normalizeForId(value) {
26
+ return path.resolve(String(value)).replace(/[\\/]+$/, "").toLowerCase();
27
+ }
28
+
29
+ function normalizeForCompare(value) {
30
+ return path.normalize(String(value)).replace(/[\\/]+$/, "").toLowerCase();
31
+ }
32
+
33
+ function hashShort(value) {
34
+ return crypto.createHash("sha1").update(value).digest("hex").slice(0, 16);
35
+ }
36
+
37
+ function fingerprintFile(filePath) {
38
+ if (!filePath || !fs.existsSync(filePath)) {
39
+ return null;
40
+ }
41
+
42
+ const stat = fs.statSync(filePath);
43
+ const hash = crypto.createHash("sha1").update(fs.readFileSync(filePath)).digest("hex");
44
+ return {
45
+ path: path.resolve(filePath),
46
+ hash,
47
+ shortHash: hash.slice(0, 12),
48
+ size: stat.size,
49
+ mtimeMs: stat.mtimeMs
50
+ };
51
+ }
52
+
53
+ function fingerprintsMatch(left, right) {
54
+ if (!left || !right) {
55
+ return false;
56
+ }
57
+
58
+ return left.hash === right.hash && left.size === right.size;
59
+ }
60
+
61
+ function buildHostId(options) {
62
+ const hostKind = options.hostKind ?? "vscode-serve-web";
63
+ return `${hostKind}-${hashShort(normalizeForId(options.fixture ?? defaultFixtureDir))}`;
64
+ }
65
+
66
+ function getHostDir(hostId) {
67
+ return path.join(managedRootDir, hostId);
68
+ }
69
+
70
+ function getMetadataPath(hostId) {
71
+ return path.join(getHostDir(hostId), "metadata.json");
72
+ }
73
+
74
+ function getWatchdogPath(hostId) {
75
+ return path.join(getHostDir(hostId), "watchdog.json");
76
+ }
77
+
78
+ function getLockDir(hostId) {
79
+ return path.join(getHostDir(hostId), "lock");
80
+ }
81
+
82
+ function ensureDir(dirPath) {
83
+ fs.mkdirSync(dirPath, { recursive: true });
84
+ }
85
+
86
+ function writeText(filePath, value) {
87
+ ensureDir(path.dirname(filePath));
88
+ fs.writeFileSync(filePath, value);
89
+ }
90
+
91
+ function appendText(filePath, value) {
92
+ ensureDir(path.dirname(filePath));
93
+ fs.appendFileSync(filePath, value);
94
+ }
95
+
96
+ function writeJsonAtomic(filePath, payload) {
97
+ ensureDir(path.dirname(filePath));
98
+ const tempPath = `${filePath}.${process.pid}.${Date.now()}.tmp`;
99
+ try {
100
+ fs.writeFileSync(tempPath, `${JSON.stringify(payload, null, 2)}\n`);
101
+ fs.renameSync(tempPath, filePath);
102
+ } catch (error) {
103
+ try {
104
+ fs.rmSync(tempPath, { force: true });
105
+ } catch {
106
+ // Best-effort cleanup only.
107
+ }
108
+ throw error;
109
+ }
110
+ }
111
+
112
+ function tail(text, maxLength = 4000) {
113
+ return text.length > maxLength ? text.slice(text.length - maxLength) : text;
114
+ }
115
+
116
+ function readTextIfExists(filePath) {
117
+ try {
118
+ return fs.readFileSync(filePath, "utf8");
119
+ } catch {
120
+ return "";
121
+ }
122
+ }
123
+
124
+ function readRecentLogText(root, maxFiles = 12, maxLength = 4000) {
125
+ if (!fs.existsSync(root)) {
126
+ return "";
127
+ }
128
+ const files = [];
129
+ collectFiles(root, files, 5);
130
+ return files
131
+ .filter((filePath) => filePath.endsWith(".log") || filePath.endsWith(".txt"))
132
+ .sort((left, right) => {
133
+ const leftTime = fs.statSync(left).mtimeMs;
134
+ const rightTime = fs.statSync(right).mtimeMs;
135
+ return rightTime - leftTime;
136
+ })
137
+ .slice(0, maxFiles)
138
+ .map((filePath) => readTextIfExists(filePath))
139
+ .join("\n")
140
+ .slice(-maxLength);
141
+ }
142
+
143
+ function collectFiles(root, files, maxDepth) {
144
+ if (maxDepth < 0) {
145
+ return;
146
+ }
147
+ let entries;
148
+ try {
149
+ entries = fs.readdirSync(root, { withFileTypes: true });
150
+ } catch {
151
+ return;
152
+ }
153
+ for (const entry of entries) {
154
+ const entryPath = path.join(root, entry.name);
155
+ if (entry.isFile()) {
156
+ files.push(entryPath);
157
+ } else if (entry.isDirectory()) {
158
+ collectFiles(entryPath, files, maxDepth - 1);
159
+ }
160
+ }
161
+ }
162
+
163
+ function isProcessAlive(pid) {
164
+ if (!Number.isInteger(pid) || pid <= 0) {
165
+ return false;
166
+ }
167
+
168
+ try {
169
+ process.kill(pid, 0);
170
+ return true;
171
+ } catch {
172
+ return false;
173
+ }
174
+ }
175
+
176
+ function workspaceMatchesFixture(entry, fixture) {
177
+ const expected = normalizeForCompare(fixture);
178
+ return (entry.workspaceFolders ?? []).some(
179
+ (workspaceFolder) => normalizeForCompare(workspaceFolder) === expected
180
+ );
181
+ }
182
+
183
+ function resolveChromePath(explicitPath) {
184
+ const candidates = [
185
+ explicitPath,
186
+ findPackagedBrowserRuntime(),
187
+ "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
188
+ "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
189
+ "C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
190
+ "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
191
+ "google-chrome",
192
+ "google-chrome-stable",
193
+ "chromium",
194
+ "chromium-browser",
195
+ "microsoft-edge"
196
+ ].filter(Boolean);
197
+
198
+ return candidates.find((candidate) => commandOrFileExists(candidate)) ?? null;
199
+ }
200
+
201
+ function findPackagedBrowserRuntime() {
202
+ const roots = [
203
+ path.join(rootDir, "runtimes", "browser"),
204
+ path.join(rootDir, "runtimes", "chrome"),
205
+ path.join(rootDir, "runtimes", "chromium")
206
+ ];
207
+ const executableNames = process.platform === "win32"
208
+ ? new Set(["chrome.exe", "chromium.exe", "msedge.exe"])
209
+ : new Set(["chrome", "chromium"]);
210
+ for (const runtimeRoot of roots) {
211
+ const found = findFirstExecutable(runtimeRoot, executableNames, 5);
212
+ if (found) {
213
+ return found;
214
+ }
215
+ }
216
+ return null;
217
+ }
218
+
219
+ function findFirstExecutable(root, executableNames, maxDepth) {
220
+ if (!fs.existsSync(root) || maxDepth < 0) {
221
+ return null;
222
+ }
223
+ let entries;
224
+ try {
225
+ entries = fs.readdirSync(root, { withFileTypes: true });
226
+ } catch {
227
+ return null;
228
+ }
229
+ for (const entry of entries) {
230
+ const entryPath = path.join(root, entry.name);
231
+ if (entry.isFile() && executableNames.has(entry.name) && commandOrFileExists(entryPath)) {
232
+ return entryPath;
233
+ }
234
+ }
235
+ for (const entry of entries) {
236
+ if (!entry.isDirectory()) {
237
+ continue;
238
+ }
239
+ const found = findFirstExecutable(path.join(root, entry.name), executableNames, maxDepth - 1);
240
+ if (found) {
241
+ return found;
242
+ }
243
+ }
244
+ return null;
245
+ }
246
+
247
+ function commandOrFileExists(candidate) {
248
+ if (candidate.includes("\\") || candidate.includes("/") || /^[A-Za-z]:/.test(candidate)) {
249
+ return fs.existsSync(candidate);
250
+ }
251
+
252
+ const escaped = String(candidate).replace(/"/g, '\\"');
253
+ const result = process.platform === "win32"
254
+ ? spawnSync("where.exe", [candidate], { encoding: "utf8", windowsHide: true, stdio: ["ignore", "pipe", "pipe"] })
255
+ : spawnSync("sh", ["-lc", `command -v "${escaped}"`], { encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] });
256
+ return result.status === 0;
257
+ }
258
+
259
+ function ideCommandEnv() {
260
+ return {
261
+ ...process.env,
262
+ DONT_PROMPT_WSL_INSTALL: process.env.DONT_PROMPT_WSL_INSTALL ?? "1"
263
+ };
264
+ }
265
+
266
+ function makeCommandSpec(command, prefixArgs = [], source = "command") {
267
+ return {
268
+ command,
269
+ prefixArgs,
270
+ source,
271
+ displayCommand: [command, ...prefixArgs].join(" ")
272
+ };
273
+ }
274
+
275
+ function resolvePackagedNodeRuntime() {
276
+ const envNodePath = process.env.AIB_NODE_PATH;
277
+ if (envNodePath && commandOrFileExists(envNodePath)) {
278
+ return { command: path.resolve(envNodePath), source: "env-node" };
279
+ }
280
+
281
+ const platformDir = nodeRuntimePlatformDir();
282
+ if (!platformDir) {
283
+ return { command: process.execPath, source: "process-node" };
284
+ }
285
+
286
+ const candidateRoots = [
287
+ path.join(rootDir, "runtimes", "node", platformDir),
288
+ path.join(rootDir, "packages", "cli", "runtimes", "node", platformDir)
289
+ ];
290
+ for (const candidateRoot of candidateRoots) {
291
+ const candidate = process.platform === "win32"
292
+ ? path.join(candidateRoot, "node.exe")
293
+ : path.join(candidateRoot, "bin", "node");
294
+ if (fs.existsSync(candidate)) {
295
+ return { command: candidate, source: "packaged-node" };
296
+ }
297
+ }
298
+
299
+ return { command: process.execPath, source: "process-node" };
300
+ }
301
+
302
+ function nodeRuntimePlatformDir() {
303
+ const arch = process.arch;
304
+ if (process.platform === "win32" && arch === "x64") {
305
+ return "win-x64";
306
+ }
307
+ if (process.platform === "linux" && arch === "x64") {
308
+ return "linux-x64";
309
+ }
310
+ if (process.platform === "linux" && arch === "arm64") {
311
+ return "linux-arm64";
312
+ }
313
+ if (process.platform === "darwin" && arch === "x64") {
314
+ return "darwin-x64";
315
+ }
316
+ if (process.platform === "darwin" && arch === "arm64") {
317
+ return "darwin-arm64";
318
+ }
319
+ return null;
320
+ }
321
+
322
+ function commandDisplayName(commandSpec) {
323
+ return typeof commandSpec === "string" ? commandSpec : commandSpec.displayCommand;
324
+ }
325
+
326
+ function commandExecutable(commandSpec) {
327
+ return typeof commandSpec === "string" ? commandSpec : commandSpec.command;
328
+ }
329
+
330
+ function commandArgs(commandSpec, args) {
331
+ return typeof commandSpec === "string" ? args : [...commandSpec.prefixArgs, ...args];
332
+ }
333
+
334
+ function runCommand(commandSpec, args, options = {}) {
335
+ return run(commandExecutable(commandSpec), commandArgs(commandSpec, args), options);
336
+ }
337
+
338
+ function startLoggedCommand(commandSpec, args, options) {
339
+ return startLoggedProcess(commandExecutable(commandSpec), commandArgs(commandSpec, args), options);
340
+ }
341
+
342
+ function resolvePackagedCodeServerCommand() {
343
+ const nodeRuntime = resolvePackagedNodeRuntime();
344
+ const entryPaths = [
345
+ path.join(rootDir, "runtimes", "code-server", "node_modules", "code-server", "out", "node", "entry.js"),
346
+ path.join(rootDir, "packages", "cli", "runtimes", "code-server", "node_modules", "code-server", "out", "node", "entry.js")
347
+ ];
348
+ for (const entryPath of entryPaths) {
349
+ if (fs.existsSync(entryPath)) {
350
+ return makeCommandSpec(nodeRuntime.command, [entryPath], nodeRuntime.source === "packaged-node"
351
+ ? "packaged-code-server-entry-bundled-node"
352
+ : "packaged-code-server-entry");
353
+ }
354
+ }
355
+ return null;
356
+ }
357
+
358
+ function resolveManagedHostKind(options, commandSpec) {
359
+ const raw = options.hostKind ?? process.env.AIB_MANAGED_HOST_KIND ?? "";
360
+ if (raw) {
361
+ if (raw !== "vscode-serve-web" && raw !== "code-server") {
362
+ throw new Error("Expected managed host kind to be vscode-serve-web or code-server.");
363
+ }
364
+ return raw;
365
+ }
366
+
367
+ const command = commandDisplayName(commandSpec);
368
+ const commandName = path.basename(String(command)).toLowerCase();
369
+ return commandName === "code-server"
370
+ || commandName === "code-server.cmd"
371
+ || command.endsWith(path.join("code-server", "out", "node", "entry.js"))
372
+ ? "code-server"
373
+ : "vscode-serve-web";
374
+ }
375
+
376
+ function resolveManagedIdeCommand(options) {
377
+ if (options.ideCommand) {
378
+ return makeCommandSpec(options.ideCommand, [], "explicit");
379
+ }
380
+
381
+ const rawHostKind = options.hostKind ?? process.env.AIB_MANAGED_HOST_KIND ?? "";
382
+ if (rawHostKind === "code-server") {
383
+ if (process.env.AIB_CODE_SERVER_PATH && commandOrFileExists(process.env.AIB_CODE_SERVER_PATH)) {
384
+ return makeCommandSpec(process.env.AIB_CODE_SERVER_PATH, [], "env-code-server");
385
+ }
386
+ const packagedCommand = resolvePackagedCodeServerCommand();
387
+ if (packagedCommand) {
388
+ return packagedCommand;
389
+ }
390
+ const candidates = [
391
+ path.join(rootDir, "runtimes", "code-server", "bin", process.platform === "win32" ? "code-server.cmd" : "code-server"),
392
+ path.join(rootDir, "runtimes", "code-server", "bin", "code-server"),
393
+ path.join(rootDir, "packages", "cli", "runtimes", "code-server", "bin", process.platform === "win32" ? "code-server.cmd" : "code-server"),
394
+ path.join(rootDir, "packages", "cli", "runtimes", "code-server", "bin", "code-server"),
395
+ "code-server"
396
+ ].filter(Boolean);
397
+ const found = candidates.find((candidate) => commandOrFileExists(candidate)) ?? "code-server";
398
+ return makeCommandSpec(found, [], "code-server-path");
399
+ }
400
+
401
+ return makeCommandSpec(resolveIdeCommand({ ide: "code" }), [], "vscode-code");
402
+ }
403
+
404
+ function buildIdeCommandEnv(runDir) {
405
+ const env = ideCommandEnv();
406
+ if (process.platform !== "win32") {
407
+ const homeDir = path.join(runDir, "home");
408
+ env.HOME = homeDir;
409
+ env.VSCODE_CLI_DATA_DIR = path.join(runDir, "cli-data");
410
+ }
411
+ return env;
412
+ }
413
+
414
+ function readJsonFile(filePath) {
415
+ try {
416
+ return JSON.parse(fs.readFileSync(filePath, "utf8"));
417
+ } catch {
418
+ return null;
419
+ }
420
+ }
421
+
422
+ function readMetadataByHostId(hostId) {
423
+ const metadataPath = getMetadataPath(hostId);
424
+ const metadata = fs.existsSync(metadataPath) ? readJsonFile(metadataPath) : null;
425
+ if (!metadata || typeof metadata !== "object") {
426
+ return null;
427
+ }
428
+ return metadata;
429
+ }
430
+
431
+ function readAllManagedHostEntries() {
432
+ if (!fs.existsSync(managedRootDir)) {
433
+ return [];
434
+ }
435
+ const entries = [];
436
+ for (const item of fs.readdirSync(managedRootDir, { withFileTypes: true })) {
437
+ if (!item.isDirectory()) {
438
+ continue;
439
+ }
440
+ const metadata = readMetadataByHostId(item.name);
441
+ if (!metadata) {
442
+ continue;
443
+ }
444
+ entries.push({
445
+ hostId: item.name,
446
+ metadata
447
+ });
448
+ }
449
+ return entries.sort((left, right) => String(left.hostId).localeCompare(String(right.hostId)));
450
+ }
451
+
452
+ function compactRuntimeStatus(status, metadata = {}) {
453
+ return {
454
+ hostId: status.hostId ?? metadata.hostId ?? null,
455
+ hostKind: status.hostKind ?? metadata.hostKind ?? null,
456
+ running: status.running === true,
457
+ healthy: status.healthy === true,
458
+ fixture: metadata.fixture ?? status.metadata?.fixture ?? null,
459
+ runDir: metadata.runDir ?? status.metadata?.runDir ?? null,
460
+ rootUrl: metadata.rootUrl ?? status.metadata?.rootUrl ?? null,
461
+ reason: status.reason ?? null
462
+ };
463
+ }
464
+
465
+ function writeMetadata(hostId, metadata) {
466
+ writeJsonAtomic(getMetadataPath(hostId), metadata);
467
+ }
468
+
469
+ function removeMetadata(hostId) {
470
+ fs.rmSync(getMetadataPath(hostId), { force: true });
471
+ }
472
+
473
+ function quoteCmdArg(value) {
474
+ const raw = String(value);
475
+ return `"${raw.replace(/%/g, "%%").replace(/"/g, '\\"')}"`;
476
+ }
477
+
478
+ function quotePowerShellString(value) {
479
+ return `'${String(value).replace(/'/g, "''")}'`;
480
+ }
481
+
482
+ function writeWindowsDetachedScript(command, args, options) {
483
+ ensureDir(path.dirname(options.stdoutPath));
484
+ ensureDir(path.dirname(options.stderrPath));
485
+ const scriptPath = `${options.stdoutPath}.cmd`;
486
+ const cwd = options.cwd ?? rootDir;
487
+ const commandLine = [
488
+ quoteCmdArg(command),
489
+ ...args.map(quoteCmdArg),
490
+ ">",
491
+ quoteCmdArg(options.stdoutPath),
492
+ "2>",
493
+ quoteCmdArg(options.stderrPath)
494
+ ].join(" ");
495
+ writeText(scriptPath, [
496
+ "@echo off",
497
+ `cd /d ${quoteCmdArg(cwd)}`,
498
+ commandLine,
499
+ ""
500
+ ].join("\r\n"));
501
+ return scriptPath;
502
+ }
503
+
504
+ function startWindowsHiddenScript(scriptPath, options) {
505
+ const cwd = options.cwd ?? rootDir;
506
+ const command = [
507
+ "$p = Start-Process -FilePath 'cmd.exe'",
508
+ `-ArgumentList @('/d','/s','/c', ${quotePowerShellString(scriptPath)})`,
509
+ `-WorkingDirectory ${quotePowerShellString(cwd)}`,
510
+ "-WindowStyle Hidden -PassThru;",
511
+ "$p.Id"
512
+ ].join(" ");
513
+ const result = spawnSync("powershell.exe", [
514
+ "-NoProfile",
515
+ "-ExecutionPolicy",
516
+ "Bypass",
517
+ "-WindowStyle",
518
+ "Hidden",
519
+ "-Command",
520
+ command
521
+ ], {
522
+ cwd,
523
+ env: options.env ?? process.env,
524
+ encoding: "utf8",
525
+ windowsHide: true,
526
+ stdio: ["ignore", "pipe", "pipe"]
527
+ });
528
+ if (result.status !== 0) {
529
+ appendText(options.stderrPath, `${result.stdout ?? ""}${result.stderr ?? ""}${result.error ? result.error.message : ""}\n`);
530
+ }
531
+ const pid = Number(String(result.stdout ?? "").trim().split(/\r?\n/).pop());
532
+ return {
533
+ pid: Number.isFinite(pid) && pid > 0 ? pid : null,
534
+ on() {},
535
+ unref() {}
536
+ };
537
+ }
538
+
539
+ function startLoggedProcess(command, args, options) {
540
+ const spawnCommand = prepareSpawnCommand(command, args);
541
+ if (options.detached === true && process.platform === "win32") {
542
+ const scriptPath = writeWindowsDetachedScript(spawnCommand.command, spawnCommand.args, options);
543
+ const child = startWindowsHiddenScript(scriptPath, options);
544
+ return {
545
+ child,
546
+ command,
547
+ args,
548
+ stdoutPath: options.stdoutPath,
549
+ stderrPath: options.stderrPath
550
+ };
551
+ }
552
+
553
+ const stdoutFd = fs.openSync(options.stdoutPath, "a");
554
+ const stderrFd = fs.openSync(options.stderrPath, "a");
555
+ const child = spawn(spawnCommand.command, spawnCommand.args, {
556
+ cwd: options.cwd ?? rootDir,
557
+ env: options.env ?? process.env,
558
+ detached: options.detached === true,
559
+ windowsHide: true,
560
+ windowsVerbatimArguments: spawnCommand.windowsVerbatimArguments ?? false,
561
+ stdio: ["ignore", stdoutFd, stderrFd]
562
+ });
563
+
564
+ child.on("error", (error) => {
565
+ appendText(options.stderrPath, `${error instanceof Error ? error.stack ?? error.message : String(error)}\n`);
566
+ });
567
+ if (options.detached === true) {
568
+ child.unref();
569
+ }
570
+
571
+ return {
572
+ child,
573
+ command,
574
+ args,
575
+ stdoutPath: options.stdoutPath,
576
+ stderrPath: options.stderrPath
577
+ };
578
+ }
579
+
580
+ function stopProcessTree(pid) {
581
+ if (!Number.isInteger(pid) || pid <= 0) {
582
+ return {
583
+ attempted: false,
584
+ pid
585
+ };
586
+ }
587
+
588
+ if (process.platform === "win32") {
589
+ const result = run("taskkill.exe", ["/pid", String(pid), "/t", "/f"], { timeoutMs: 10_000 });
590
+ return {
591
+ attempted: true,
592
+ pid,
593
+ status: result.status,
594
+ error: result.error,
595
+ stdoutTail: tail(result.stdout, 1000),
596
+ stderrTail: tail(result.stderr, 1000)
597
+ };
598
+ }
599
+
600
+ try {
601
+ if (process.platform !== "win32") {
602
+ process.kill(-pid, "SIGTERM");
603
+ } else {
604
+ process.kill(pid, "SIGTERM");
605
+ }
606
+ if (process.platform !== "win32" && !waitForProcessExit(pid, 2000)) {
607
+ try {
608
+ process.kill(-pid, "SIGKILL");
609
+ } catch {
610
+ try {
611
+ process.kill(pid, "SIGKILL");
612
+ } catch {
613
+ // Keep the SIGTERM result; this is best-effort cleanup.
614
+ }
615
+ }
616
+ waitForProcessExit(pid, 1000);
617
+ }
618
+ return {
619
+ attempted: true,
620
+ pid,
621
+ status: 0,
622
+ error: null
623
+ };
624
+ } catch (error) {
625
+ if (process.platform !== "win32") {
626
+ try {
627
+ process.kill(pid, "SIGTERM");
628
+ if (!waitForProcessExit(pid, 2000)) {
629
+ try {
630
+ process.kill(pid, "SIGKILL");
631
+ } catch {
632
+ // Keep the SIGTERM result; this is best-effort cleanup.
633
+ }
634
+ waitForProcessExit(pid, 1000);
635
+ }
636
+ return {
637
+ attempted: true,
638
+ pid,
639
+ status: 0,
640
+ error: null
641
+ };
642
+ } catch {
643
+ // Keep the original process-group error for diagnostics.
644
+ }
645
+ }
646
+ return {
647
+ attempted: true,
648
+ pid,
649
+ status: 1,
650
+ error: error instanceof Error ? error.message : String(error)
651
+ };
652
+ }
653
+ }
654
+
655
+ function stopProcessesByCommandLineFragment(fragment) {
656
+ if (process.platform !== "win32" || !fragment) {
657
+ return {
658
+ attempted: false,
659
+ fragment
660
+ };
661
+ }
662
+
663
+ const escaped = String(fragment).replace(/'/g, "''");
664
+ const script = [
665
+ "$fragment = '" + escaped + "'",
666
+ "Get-CimInstance Win32_Process |",
667
+ "Where-Object { $_.CommandLine -and $_.CommandLine.Contains($fragment) } |",
668
+ "ForEach-Object { try { Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop; $_.ProcessId } catch {} }"
669
+ ].join(" ");
670
+ const result = run("powershell.exe", ["-NoProfile", "-Command", script], { timeoutMs: 10_000 });
671
+ return {
672
+ attempted: true,
673
+ fragment,
674
+ status: result.status,
675
+ error: result.error,
676
+ stdoutTail: tail(result.stdout, 1000),
677
+ stderrTail: tail(result.stderr, 1000)
678
+ };
679
+ }
680
+
681
+ function stopOrphanedPackagedRuntimeProcesses(activeRunDirs = []) {
682
+ if (process.platform !== "win32") {
683
+ return {
684
+ attempted: false,
685
+ reason: "unsupported-platform"
686
+ };
687
+ }
688
+
689
+ const escapedRoot = rootDir.replace(/'/g, "''");
690
+ const activeList = activeRunDirs
691
+ .filter(Boolean)
692
+ .map((item) => "'" + String(item).replace(/'/g, "''") + "'")
693
+ .join(",");
694
+ const script = [
695
+ "$root = '" + escapedRoot + "'",
696
+ "$active = @(" + activeList + ")",
697
+ "$runtimePattern = [regex]::Escape($root + '\\runtimes\\')",
698
+ "$runPattern = [regex]::Escape($root + '\\.tmp\\managed-host-runs\\')",
699
+ "Get-CimInstance Win32_Process |",
700
+ "Where-Object {",
701
+ " $cmd = $_.CommandLine",
702
+ " $cmd -and",
703
+ " $cmd -match $runtimePattern -and",
704
+ " $cmd -match $runPattern -and",
705
+ " -not ($active | Where-Object { $_ -and $cmd.Contains($_) })",
706
+ "} |",
707
+ "ForEach-Object { try { Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop; $_.ProcessId } catch {} }"
708
+ ].join(" ");
709
+ const result = run("powershell.exe", ["-NoProfile", "-Command", script], { timeoutMs: 10_000 });
710
+ return {
711
+ attempted: true,
712
+ status: result.status,
713
+ error: result.error,
714
+ stdoutTail: tail(result.stdout, 1000),
715
+ stderrTail: tail(result.stderr, 1000)
716
+ };
717
+ }
718
+
719
+ function stopAibOwnedRuntimeProcesses() {
720
+ if (process.platform !== "win32") {
721
+ return stopAibOwnedRuntimeProcessesUnix();
722
+ }
723
+
724
+ const roots = [
725
+ rootDir,
726
+ path.join(rootDir, "packages", "cli")
727
+ ].map((item) => String(item).replace(/'/g, "''"));
728
+ const script = [
729
+ "$roots = @(" + roots.map((item) => "'" + item + "'").join(",") + ")",
730
+ "$processes = @(Get-CimInstance Win32_Process | Where-Object {",
731
+ " $cmd = $_.CommandLine",
732
+ " if (-not $cmd) { return $false }",
733
+ " $underAibRoot = [bool]($roots | Where-Object { $_ -and $cmd.Contains($_) })",
734
+ " $hasRunMarker = $cmd.Contains('managed-host-runs')",
735
+ " $isAibWatchdog = $cmd.Contains('manage-serve-web-host.cjs') -and $cmd.Contains('watchdog') -and $underAibRoot",
736
+ " $isPackagedRuntime = $underAibRoot -and ($cmd.Contains('\\runtimes\\code-server\\') -or $cmd.Contains('\\runtimes\\browser\\'))",
737
+ " $isAibDetachedWrapper = $underAibRoot -and ($cmd.Contains('serve-web.stdout.txt.cmd') -or $cmd.Contains('headless-browser.stdout.txt.cmd') -or $cmd.Contains('watchdog.stdout.txt.cmd'))",
738
+ " ($hasRunMarker -and $underAibRoot) -or $isAibWatchdog -or $isPackagedRuntime -or $isAibDetachedWrapper",
739
+ "})",
740
+ "$processes | ForEach-Object { try { Stop-Process -Id $_.ProcessId -Force -ErrorAction Stop; $_.ProcessId } catch {} }"
741
+ ].join("\n");
742
+ const result = run("powershell.exe", ["-NoProfile", "-Command", script], { timeoutMs: 15_000 });
743
+ const killed = result.stdout
744
+ .split(/\r?\n/)
745
+ .map((line) => line.trim())
746
+ .filter(Boolean).length;
747
+ return {
748
+ attempted: true,
749
+ killed,
750
+ status: result.status,
751
+ error: result.error,
752
+ stdoutTail: tail(result.stdout, 1000),
753
+ stderrTail: tail(result.stderr, 1000)
754
+ };
755
+ }
756
+
757
+ function stopAibOwnedRuntimeProcessesUnix() {
758
+ const roots = [
759
+ rootDir,
760
+ path.join(rootDir, "packages", "cli")
761
+ ].map((item) => String(item));
762
+ const result = run("ps", ["-eo", "pid=,args="], { timeoutMs: 10_000 });
763
+ if (result.status !== 0) {
764
+ return {
765
+ attempted: true,
766
+ killed: 0,
767
+ status: result.status,
768
+ error: result.error,
769
+ stdoutTail: tail(result.stdout, 1000),
770
+ stderrTail: tail(result.stderr, 1000)
771
+ };
772
+ }
773
+
774
+ const currentPid = process.pid;
775
+ const parentPid = process.ppid;
776
+ const pids = result.stdout
777
+ .split(/\r?\n/)
778
+ .map((line) => {
779
+ const match = line.match(/^\s*(\d+)\s+(.*)$/);
780
+ if (!match) {
781
+ return null;
782
+ }
783
+ return {
784
+ pid: Number(match[1]),
785
+ commandLine: match[2] ?? ""
786
+ };
787
+ })
788
+ .filter(Boolean)
789
+ .filter((row) => row.pid !== currentPid && row.pid !== parentPid)
790
+ .filter((row) => isAibOwnedRuntimeCommandLine(row.commandLine, roots))
791
+ .map((row) => row.pid);
792
+
793
+ const uniquePids = [...new Set(pids)].filter((pid) => Number.isInteger(pid) && pid > 0);
794
+ let killed = 0;
795
+ const errors = [];
796
+ for (const pid of uniquePids) {
797
+ try {
798
+ process.kill(pid, "SIGTERM");
799
+ killed += 1;
800
+ } catch (error) {
801
+ errors.push(`${pid}: ${error instanceof Error ? error.message : String(error)}`);
802
+ }
803
+ }
804
+ if (killed > 0) {
805
+ sleepSync(250);
806
+ }
807
+ for (const pid of uniquePids) {
808
+ if (!isProcessAlive(pid)) {
809
+ continue;
810
+ }
811
+ try {
812
+ process.kill(pid, "SIGKILL");
813
+ } catch (error) {
814
+ errors.push(`${pid}: ${error instanceof Error ? error.message : String(error)}`);
815
+ }
816
+ }
817
+
818
+ return {
819
+ attempted: true,
820
+ killed,
821
+ status: errors.length === 0 ? 0 : 1,
822
+ error: errors.join("; ") || null
823
+ };
824
+ }
825
+
826
+ function isAibOwnedRuntimeCommandLine(commandLine, roots) {
827
+ if (!commandLine) {
828
+ return false;
829
+ }
830
+ const underAibRoot = roots.some((root) => root && commandLine.includes(root));
831
+ if (!underAibRoot) {
832
+ return false;
833
+ }
834
+ const hasRunMarker = commandLine.includes("managed-host-runs");
835
+ const isAibWatchdog = commandLine.includes("manage-serve-web-host.cjs") && commandLine.includes("watchdog");
836
+ const isPackagedRuntime =
837
+ commandLine.includes("/runtimes/code-server/") ||
838
+ commandLine.includes("\\runtimes\\code-server\\") ||
839
+ commandLine.includes("/runtimes/browser/") ||
840
+ commandLine.includes("\\runtimes\\browser\\");
841
+ return hasRunMarker || isAibWatchdog || isPackagedRuntime;
842
+ }
843
+
844
+ function sleepSync(ms) {
845
+ try {
846
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
847
+ } catch {
848
+ // Best-effort delay only.
849
+ }
850
+ }
851
+
852
+ function waitForProcessExit(pid, timeoutMs) {
853
+ const deadline = Date.now() + timeoutMs;
854
+ while (Date.now() < deadline) {
855
+ if (!isProcessAlive(pid)) {
856
+ return true;
857
+ }
858
+ sleepSync(50);
859
+ }
860
+ return !isProcessAlive(pid);
861
+ }
862
+
863
+ function readTtlMs(value, fallbackMs = 15 * 60_000) {
864
+ if (value === null || value === undefined || value === "") {
865
+ return fallbackMs;
866
+ }
867
+ const parsed = Number(value);
868
+ return Number.isFinite(parsed) ? parsed : fallbackMs;
869
+ }
870
+
871
+ function metadataLastActivityMs(metadata) {
872
+ const raw = metadata?.lastActivityAt ?? metadata?.updatedAt ?? metadata?.startedAt;
873
+ const parsed = typeof raw === "string" ? Date.parse(raw) : NaN;
874
+ return Number.isFinite(parsed) ? parsed : 0;
875
+ }
876
+
877
+ function isMetadataExpired(metadata, ttlMs, now = Date.now()) {
878
+ if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
879
+ return false;
880
+ }
881
+ const lastActivityMs = metadataLastActivityMs(metadata);
882
+ return lastActivityMs > 0 && now - lastActivityMs > ttlMs;
883
+ }
884
+
885
+ async function tryFetch(url) {
886
+ try {
887
+ const response = await fetch(url);
888
+ return {
889
+ ok: response.ok,
890
+ status: response.status,
891
+ contentType: response.headers.get("content-type")
892
+ };
893
+ } catch (error) {
894
+ return {
895
+ ok: false,
896
+ error: error instanceof Error ? error.message : String(error)
897
+ };
898
+ }
899
+ }
900
+
901
+ async function waitFor(name, timeoutMs, pollMs, getState, isReady) {
902
+ const startedAt = Date.now();
903
+ const attempts = [];
904
+
905
+ while (Date.now() - startedAt <= timeoutMs) {
906
+ const state = await getState();
907
+ attempts.push(state);
908
+ if (isReady(state)) {
909
+ return {
910
+ ok: true,
911
+ durationMs: Date.now() - startedAt,
912
+ attempts: attempts.length,
913
+ state
914
+ };
915
+ }
916
+ await sleep(pollMs);
917
+ }
918
+
919
+ return {
920
+ ok: false,
921
+ durationMs: Date.now() - startedAt,
922
+ attempts: attempts.length,
923
+ error: `${name} did not become ready within ${timeoutMs}ms.`,
924
+ lastState: attempts.at(-1) ?? null
925
+ };
926
+ }
927
+
928
+ async function allocatePort() {
929
+ return new Promise((resolve, reject) => {
930
+ const server = net.createServer();
931
+ server.once("error", reject);
932
+ server.listen(0, "127.0.0.1", () => {
933
+ const address = server.address();
934
+ const port = address && typeof address === "object" ? address.port : null;
935
+ server.close(() => {
936
+ if (Number.isInteger(port)) {
937
+ resolve(port);
938
+ } else {
939
+ reject(new Error("Failed to allocate a TCP port."));
940
+ }
941
+ });
942
+ });
943
+ });
944
+ }
945
+
946
+ async function acquireLock(hostId, timeoutMs = 15_000, staleMs = 120_000) {
947
+ const lockDir = getLockDir(hostId);
948
+ const startedAt = Date.now();
949
+ ensureDir(path.dirname(lockDir));
950
+
951
+ while (Date.now() - startedAt <= timeoutMs) {
952
+ try {
953
+ fs.mkdirSync(lockDir);
954
+ writeJson(path.join(lockDir, "owner.json"), {
955
+ pid: process.pid,
956
+ acquiredAt: new Date().toISOString()
957
+ });
958
+ return {
959
+ hostId,
960
+ lockDir,
961
+ release() {
962
+ fs.rmSync(lockDir, { recursive: true, force: true });
963
+ }
964
+ };
965
+ } catch (error) {
966
+ if (error && error.code === "EEXIST") {
967
+ const stat = fs.statSync(lockDir);
968
+ if (Date.now() - stat.mtimeMs > staleMs) {
969
+ fs.rmSync(lockDir, { recursive: true, force: true });
970
+ continue;
971
+ }
972
+ await sleep(250);
973
+ continue;
974
+ }
975
+ throw error;
976
+ }
977
+ }
978
+
979
+ throw new Error(`Timed out acquiring managed host lock: ${lockDir}`);
980
+ }
981
+
982
+ function getMatchingRegistryEntry(metadata) {
983
+ if (!metadata || typeof metadata.instanceId !== "string") {
984
+ return null;
985
+ }
986
+
987
+ return readRegistryEntries().find((entry) => entry.instanceId === metadata.instanceId) ?? null;
988
+ }
989
+
990
+ async function getManagedServeWebHostStatus(options = {}) {
991
+ if (options.all === true) {
992
+ return getAllManagedServeWebHostStatuses(options);
993
+ }
994
+
995
+ const fixture = path.resolve(options.fixture ?? defaultFixtureDir);
996
+ const codeCommand = resolveManagedIdeCommand(options);
997
+ const hostKind = resolveManagedHostKind(options, codeCommand);
998
+ const hostId = options.hostId ?? buildHostId({ fixture, hostKind });
999
+ const expectedVsix = options.vsix ? fingerprintFile(path.resolve(options.vsix)) : null;
1000
+ const metadata = readMetadataByHostId(hostId);
1001
+ if (!metadata) {
1002
+ return {
1003
+ ok: true,
1004
+ hostId,
1005
+ running: false,
1006
+ healthy: false,
1007
+ reason: "metadata-not-found"
1008
+ };
1009
+ }
1010
+
1011
+ const registryEntry = getMatchingRegistryEntry(metadata);
1012
+ const rootUrl = metadata.rootUrl ?? (Number.isInteger(metadata.port) ? `http://127.0.0.1:${metadata.port}` : null);
1013
+ const http = rootUrl ? await tryFetch(rootUrl) : { ok: false, error: "missing rootUrl" };
1014
+ const serveWebAlive = isProcessAlive(metadata.serveWebPid);
1015
+ const browserAlive = isProcessAlive(metadata.browserPid);
1016
+ const extensionHostAlive = registryEntry ? isProcessAlive(registryEntry.pid) : false;
1017
+ const workspaceMatches = registryEntry ? workspaceMatchesFixture(registryEntry, fixture) : false;
1018
+ const vsixMatches = expectedVsix ? fingerprintsMatch(metadata.vsixFingerprint, expectedVsix) : true;
1019
+ const healthy = serveWebAlive && browserAlive && extensionHostAlive && http.ok === true && workspaceMatches && vsixMatches;
1020
+
1021
+ return {
1022
+ ok: true,
1023
+ hostId,
1024
+ running: serveWebAlive || browserAlive || extensionHostAlive,
1025
+ healthy,
1026
+ metadata,
1027
+ health: {
1028
+ serveWebAlive,
1029
+ browserAlive,
1030
+ extensionHostAlive,
1031
+ workspaceMatches,
1032
+ vsixMatches,
1033
+ expectedVsix,
1034
+ http,
1035
+ registryEntry
1036
+ }
1037
+ };
1038
+ }
1039
+
1040
+ async function getAllManagedServeWebHostStatuses(options = {}) {
1041
+ const entries = readAllManagedHostEntries();
1042
+ const runtimes = [];
1043
+ for (const entry of entries) {
1044
+ const metadata = entry.metadata;
1045
+ const status = await getManagedServeWebHostStatus({
1046
+ ...options,
1047
+ all: false,
1048
+ hostId: entry.hostId,
1049
+ fixture: metadata.fixture ?? defaultFixtureDir,
1050
+ vsix: metadata.vsix,
1051
+ hostKind: metadata.hostKind
1052
+ });
1053
+ runtimes.push(compactRuntimeStatus(status, metadata));
1054
+ }
1055
+ return {
1056
+ ok: true,
1057
+ all: true,
1058
+ count: runtimes.length,
1059
+ running: runtimes.filter((runtime) => runtime.running === true).length,
1060
+ healthy: runtimes.filter((runtime) => runtime.healthy === true).length,
1061
+ runtimes
1062
+ };
1063
+ }
1064
+
1065
+ async function startManagedServeWebHost(options = {}) {
1066
+ const startedAt = Date.now();
1067
+ const fixture = path.resolve(options.fixture ?? defaultFixtureDir);
1068
+ const ttlMs = readTtlMs(options.ttlMs);
1069
+ const intervalMs = Number.isFinite(Number(options.intervalMs)) && Number(options.intervalMs) > 0
1070
+ ? Number(options.intervalMs)
1071
+ : 30_000;
1072
+ const waitMs = Number.isInteger(options.waitMs) ? options.waitMs : 45_000;
1073
+ const pollMs = Number.isInteger(options.pollMs) ? options.pollMs : 500;
1074
+ const vsix = path.resolve(options.vsix ?? defaultVsixPath);
1075
+ const vsixFingerprint = fingerprintFile(vsix);
1076
+ const reuse = options.reuse !== false;
1077
+ const codeCommand = resolveManagedIdeCommand(options);
1078
+ const hostKind = resolveManagedHostKind(options, codeCommand);
1079
+ const hostId = options.hostId ?? buildHostId({ fixture, hostKind });
1080
+ const runDir = path.resolve(options.runDir ?? path.join(defaultRunsDir, `${hostId}-${timestampForPath()}`));
1081
+ const logsDir = path.join(runDir, "logs");
1082
+ const stepsDir = path.join(runDir, "steps");
1083
+ const userDataDir = path.join(runDir, "user-data");
1084
+ const serverDataDir = path.join(runDir, "server-data");
1085
+ const extensionsDir = path.join(runDir, "extensions");
1086
+ const activeExtensionsDir = hostKind === "code-server" || process.platform === "win32"
1087
+ ? extensionsDir
1088
+ : path.join(serverDataDir, "extensions");
1089
+ const ideEnv = buildIdeCommandEnv(runDir);
1090
+ const steps = [];
1091
+ const failures = [];
1092
+ const beforeRegistry = readRegistryEntries();
1093
+ const beforeInstanceIds = new Set(beforeRegistry.map((entry) => entry.instanceId));
1094
+ let serveWebProcess = null;
1095
+ let browserProcess = null;
1096
+ let metadata = null;
1097
+
1098
+ ensureDir(logsDir);
1099
+ ensureDir(stepsDir);
1100
+ await cleanupExpiredManagedServeWebHosts({ ttlMs });
1101
+
1102
+ async function step(name, fn) {
1103
+ const stepIndex = String(steps.length + 1).padStart(2, "0");
1104
+ const stepPath = path.join(stepsDir, `${stepIndex}-${name.replace(/[^a-z0-9]+/gi, "-").toLowerCase()}.json`);
1105
+ const startedStepAt = Date.now();
1106
+ const record = {
1107
+ name,
1108
+ ok: false,
1109
+ startedAt: new Date(startedStepAt).toISOString()
1110
+ };
1111
+
1112
+ try {
1113
+ record.detail = await fn();
1114
+ record.ok = record.detail?.ok !== false;
1115
+ if (!record.ok) {
1116
+ failures.push(`${name}: ${record.detail?.error ?? "failed"}`);
1117
+ }
1118
+ } catch (error) {
1119
+ record.ok = false;
1120
+ record.error = error instanceof Error ? error.message : String(error);
1121
+ failures.push(`${name}: ${record.error}`);
1122
+ } finally {
1123
+ record.durationMs = Date.now() - startedStepAt;
1124
+ record.finishedAt = new Date().toISOString();
1125
+ record.path = stepPath;
1126
+ writeJson(stepPath, record);
1127
+ steps.push(record);
1128
+ }
1129
+
1130
+ return record;
1131
+ }
1132
+
1133
+ const lock = await acquireLock(hostId);
1134
+ try {
1135
+ if (reuse) {
1136
+ const existingStatus = await getManagedServeWebHostStatus({ fixture, hostId, vsix, ideCommand: options.ideCommand, hostKind });
1137
+ if (existingStatus.healthy) {
1138
+ const updatedMetadata = {
1139
+ ...existingStatus.metadata,
1140
+ updatedAt: new Date().toISOString(),
1141
+ lastActivityAt: new Date().toISOString(),
1142
+ ttlMs,
1143
+ watchdogIntervalMs: intervalMs
1144
+ };
1145
+ writeMetadata(hostId, updatedMetadata);
1146
+ startManagedServeWebWatchdog({ fixture, hostId, ttlMs, intervalMs, logsDir: existingStatus.metadata.logsDir ?? logsDir, ideCommand: options.ideCommand, hostKind });
1147
+ return buildStartSummary({
1148
+ ok: true,
1149
+ reused: true,
1150
+ startedAt,
1151
+ hostId,
1152
+ fixture,
1153
+ vsix,
1154
+ codeCommand,
1155
+ hostKind,
1156
+ runDir: existingStatus.metadata.runDir ?? runDir,
1157
+ logsDir: existingStatus.metadata.logsDir ?? logsDir,
1158
+ stepsDir: existingStatus.metadata.stepsDir ?? stepsDir,
1159
+ metadata: updatedMetadata,
1160
+ steps,
1161
+ failures
1162
+ });
1163
+ }
1164
+ if (existingStatus.metadata) {
1165
+ await stopManagedServeWebHost({ fixture, hostId, removeMetadataOnlyIfKnown: true });
1166
+ }
1167
+ } else {
1168
+ await stopManagedServeWebHost({ fixture, hostId, removeMetadataOnlyIfKnown: true });
1169
+ }
1170
+
1171
+ await step("prepare profile", async () => {
1172
+ ensureDir(path.join(runDir, "home"));
1173
+ ensureDir(path.join(runDir, "cli-data"));
1174
+ ensureDir(path.join(userDataDir, "User"));
1175
+ ensureDir(path.join(serverDataDir, "data", "User"));
1176
+ ensureDir(serverDataDir);
1177
+ ensureDir(extensionsDir);
1178
+ ensureDir(activeExtensionsDir);
1179
+ const settings = {
1180
+ "security.workspace.trust.enabled": false,
1181
+ "workbench.startupEditor": "none"
1182
+ };
1183
+ writeJson(path.join(userDataDir, "User", "settings.json"), settings);
1184
+ writeJson(path.join(serverDataDir, "data", "User", "settings.json"), settings);
1185
+ return {
1186
+ ok: true,
1187
+ runDir,
1188
+ userDataDir,
1189
+ serverDataDir,
1190
+ extensionsDir
1191
+ };
1192
+ });
1193
+
1194
+ await step("install extension", async () => {
1195
+ if (!fs.existsSync(vsix)) {
1196
+ return {
1197
+ ok: false,
1198
+ error: `VSIX does not exist: ${vsix}`
1199
+ };
1200
+ }
1201
+ const result = runCommand(codeCommand, [
1202
+ "--extensions-dir",
1203
+ activeExtensionsDir,
1204
+ "--install-extension",
1205
+ vsix,
1206
+ "--force"
1207
+ ], { timeoutMs: 60_000, env: ideEnv });
1208
+ const stdoutPath = path.join(logsDir, "install-extension.stdout.txt");
1209
+ const stderrPath = path.join(logsDir, "install-extension.stderr.txt");
1210
+ writeText(stdoutPath, result.stdout);
1211
+ writeText(stderrPath, result.stderr);
1212
+ return {
1213
+ ok: result.status === 0,
1214
+ status: result.status,
1215
+ durationMs: result.durationMs,
1216
+ stdoutPath,
1217
+ stderrPath,
1218
+ stdoutTail: tail(result.stdout),
1219
+ stderrTail: tail(result.stderr),
1220
+ error: result.error
1221
+ };
1222
+ });
1223
+ if (failures.length > 0) {
1224
+ return buildStartSummary({ ok: false, reused: false, startedAt, hostId, fixture, vsix, codeCommand, hostKind, runDir, logsDir, stepsDir, metadata, steps, failures });
1225
+ }
1226
+
1227
+ const port = Number.isInteger(options.port) ? options.port : await allocatePort();
1228
+ const rootUrl = `http://127.0.0.1:${port}`;
1229
+
1230
+ const serveWebStdoutPath = path.join(logsDir, "serve-web.stdout.txt");
1231
+ const serveWebStderrPath = path.join(logsDir, "serve-web.stderr.txt");
1232
+
1233
+ await step("start serve-web", async () => {
1234
+ const args = hostKind === "code-server"
1235
+ ? [
1236
+ "--auth",
1237
+ "none",
1238
+ "--bind-addr",
1239
+ `127.0.0.1:${port}`,
1240
+ "--disable-workspace-trust",
1241
+ "--disable-telemetry",
1242
+ "--disable-update-check",
1243
+ "--user-data-dir",
1244
+ userDataDir,
1245
+ "--extensions-dir",
1246
+ activeExtensionsDir,
1247
+ fixture
1248
+ ]
1249
+ : [
1250
+ "--user-data-dir",
1251
+ userDataDir,
1252
+ ...(process.platform === "win32" ? ["--extensions-dir", activeExtensionsDir] : []),
1253
+ "serve-web",
1254
+ "--accept-server-license-terms",
1255
+ "--host",
1256
+ "127.0.0.1",
1257
+ "--port",
1258
+ String(port),
1259
+ "--without-connection-token",
1260
+ "--default-folder",
1261
+ fixture,
1262
+ "--server-data-dir",
1263
+ serverDataDir,
1264
+ "--log",
1265
+ "trace",
1266
+ ...(process.platform !== "win32" ? ["--cli-data-dir", ideEnv.VSCODE_CLI_DATA_DIR ?? path.join(runDir, "cli-data")] : [])
1267
+ ];
1268
+ serveWebProcess = startLoggedCommand(codeCommand, args, {
1269
+ stdoutPath: serveWebStdoutPath,
1270
+ stderrPath: serveWebStderrPath,
1271
+ detached: true,
1272
+ env: ideEnv
1273
+ });
1274
+ return {
1275
+ ok: true,
1276
+ pid: serveWebProcess.child.pid ?? null,
1277
+ command: commandDisplayName(codeCommand),
1278
+ commandSource: codeCommand.source ?? null,
1279
+ hostKind,
1280
+ args,
1281
+ stdoutPath: serveWebStdoutPath,
1282
+ stderrPath: serveWebStderrPath
1283
+ };
1284
+ });
1285
+
1286
+ await step("wait for serve-web http", async () => {
1287
+ return waitFor(
1288
+ "serve-web http",
1289
+ waitMs,
1290
+ pollMs,
1291
+ () => tryFetch(rootUrl),
1292
+ (state) => state.ok === true
1293
+ );
1294
+ });
1295
+
1296
+ await step("wait for extension host agent", async () => {
1297
+ return waitFor(
1298
+ "extension host agent",
1299
+ waitMs,
1300
+ pollMs,
1301
+ () => {
1302
+ const stdout = readTextIfExists(serveWebStdoutPath);
1303
+ const stderr = readTextIfExists(serveWebStderrPath);
1304
+ const userDataLogs = readRecentLogText(path.join(userDataDir, "logs"));
1305
+ const serverDataLogs = readRecentLogText(path.join(serverDataDir, "logs"));
1306
+ const combined = `${stdout}\n${stderr}\n${userDataLogs}\n${serverDataLogs}`;
1307
+ return {
1308
+ ready: combined.includes("Extension host agent started")
1309
+ || combined.includes("Extension host agent listening"),
1310
+ stdoutTail: tail(stdout, 1000),
1311
+ stderrTail: tail(stderr, 1000),
1312
+ logTail: tail(`${userDataLogs}\n${serverDataLogs}`, 1000)
1313
+ };
1314
+ },
1315
+ (state) => state.ready === true
1316
+ );
1317
+ });
1318
+ if (failures.length > 0) {
1319
+ stopProcessTree(serveWebProcess?.child?.pid);
1320
+ return buildStartSummary({ ok: false, reused: false, startedAt, hostId, fixture, vsix, codeCommand, hostKind, runDir, logsDir, stepsDir, metadata, steps, failures });
1321
+ }
1322
+
1323
+ await step("open headless browser client", async () => {
1324
+ const chromePath = resolveChromePath(options.chromePath);
1325
+ if (!chromePath) {
1326
+ return {
1327
+ ok: false,
1328
+ error: "Chrome or Edge executable was not found."
1329
+ };
1330
+ }
1331
+
1332
+ const profileDir = path.join(runDir, "headless-browser-profile");
1333
+ const stdoutPath = path.join(logsDir, "headless-browser.stdout.txt");
1334
+ const stderrPath = path.join(logsDir, "headless-browser.stderr.txt");
1335
+ const url = `${rootUrl}/?folder=${encodeURIComponent(fixture)}`;
1336
+ const browserArgs = [
1337
+ "--headless=new",
1338
+ "--disable-gpu",
1339
+ "--disable-dev-shm-usage",
1340
+ "--disable-background-networking",
1341
+ "--disable-sync",
1342
+ "--no-sandbox",
1343
+ "--remote-debugging-port=0",
1344
+ "--no-first-run",
1345
+ "--no-default-browser-check",
1346
+ `--user-data-dir=${profileDir}`,
1347
+ url
1348
+ ];
1349
+ if (process.platform === "win32") {
1350
+ const scriptPath = writeWindowsDetachedScript(chromePath, browserArgs, {
1351
+ cwd: rootDir,
1352
+ stdoutPath,
1353
+ stderrPath
1354
+ });
1355
+ browserProcess = startWindowsHiddenScript(scriptPath, {
1356
+ cwd: rootDir,
1357
+ stdoutPath,
1358
+ stderrPath
1359
+ });
1360
+ } else {
1361
+ const stdoutFd = fs.openSync(stdoutPath, "a");
1362
+ const stderrFd = fs.openSync(stderrPath, "a");
1363
+ browserProcess = spawn(chromePath, browserArgs, {
1364
+ cwd: rootDir,
1365
+ detached: true,
1366
+ windowsHide: true,
1367
+ stdio: ["ignore", stdoutFd, stderrFd]
1368
+ });
1369
+ }
1370
+ browserProcess.on("error", () => {
1371
+ // Later registry/health waits surface browser startup failures.
1372
+ });
1373
+ browserProcess.unref();
1374
+
1375
+ return {
1376
+ ok: true,
1377
+ pid: browserProcess.pid ?? null,
1378
+ chromePath,
1379
+ profileDir,
1380
+ devToolsActivePortPath: path.join(profileDir, "DevToolsActivePort"),
1381
+ stdoutPath,
1382
+ stderrPath,
1383
+ url
1384
+ };
1385
+ });
1386
+
1387
+ await step("wait for bridge registry entry", async () => {
1388
+ const waitResult = await waitFor(
1389
+ "fresh bridge registry entry",
1390
+ waitMs,
1391
+ pollMs,
1392
+ () => {
1393
+ const entries = readRegistryEntries();
1394
+ const matchingFreshEntries = entries.filter(
1395
+ (entry) => !beforeInstanceIds.has(entry.instanceId) && workspaceMatchesFixture(entry, fixture)
1396
+ );
1397
+ return {
1398
+ ok: matchingFreshEntries.length > 0,
1399
+ entry: matchingFreshEntries[0] ?? null,
1400
+ entryCount: entries.length,
1401
+ matchingFreshCount: matchingFreshEntries.length
1402
+ };
1403
+ },
1404
+ (state) => state.ok === true
1405
+ );
1406
+ return waitResult;
1407
+ });
1408
+ if (failures.length > 0) {
1409
+ stopProcessTree(browserProcess?.pid);
1410
+ stopProcessTree(serveWebProcess?.child?.pid);
1411
+ return buildStartSummary({ ok: false, reused: false, startedAt, hostId, fixture, vsix, codeCommand, hostKind, runDir, logsDir, stepsDir, metadata, steps, failures });
1412
+ }
1413
+
1414
+ const registryEntry = steps.at(-1)?.detail?.state?.entry ?? null;
1415
+ metadata = {
1416
+ hostId,
1417
+ kind: "serve-web-headless-client",
1418
+ hostKind,
1419
+ fixture,
1420
+ vsix,
1421
+ vsixFingerprint,
1422
+ codeCommand: commandDisplayName(codeCommand),
1423
+ codeCommandSource: codeCommand.source ?? null,
1424
+ runDir,
1425
+ logsDir,
1426
+ stepsDir,
1427
+ port,
1428
+ rootUrl,
1429
+ serveWebPid: serveWebProcess?.child?.pid ?? null,
1430
+ browserPid: browserProcess?.pid ?? null,
1431
+ instanceId: registryEntry?.instanceId ?? null,
1432
+ registryPath: registryEntry?.registryPath ?? null,
1433
+ workspaceFolders: registryEntry?.workspaceFolders ?? [],
1434
+ startedAt: new Date(startedAt).toISOString(),
1435
+ updatedAt: new Date().toISOString()
1436
+ };
1437
+ metadata.lastActivityAt = metadata.updatedAt;
1438
+ metadata.ttlMs = ttlMs;
1439
+ metadata.watchdogIntervalMs = intervalMs;
1440
+ writeMetadata(hostId, metadata);
1441
+ startManagedServeWebWatchdog({ fixture, hostId, ttlMs, intervalMs, logsDir, ideCommand: options.ideCommand, hostKind });
1442
+
1443
+ return buildStartSummary({
1444
+ ok: true,
1445
+ reused: false,
1446
+ startedAt,
1447
+ hostId,
1448
+ fixture,
1449
+ vsix,
1450
+ codeCommand: commandDisplayName(codeCommand),
1451
+ codeCommandSource: codeCommand.source ?? null,
1452
+ hostKind,
1453
+ runDir,
1454
+ logsDir,
1455
+ stepsDir,
1456
+ metadata,
1457
+ registryEntry,
1458
+ steps,
1459
+ failures
1460
+ });
1461
+ } finally {
1462
+ lock.release();
1463
+ }
1464
+ }
1465
+
1466
+ async function stopManagedServeWebHost(options = {}) {
1467
+ if (options.all === true) {
1468
+ return stopAllManagedServeWebHosts(options);
1469
+ }
1470
+
1471
+ const fixture = path.resolve(options.fixture ?? defaultFixtureDir);
1472
+ const codeCommand = resolveManagedIdeCommand(options);
1473
+ const hostKind = resolveManagedHostKind(options, codeCommand);
1474
+ const hostId = options.hostId ?? buildHostId({ fixture, hostKind });
1475
+ const metadata = readMetadataByHostId(hostId);
1476
+ if (!metadata) {
1477
+ return {
1478
+ ok: true,
1479
+ hostId,
1480
+ hostKind,
1481
+ stopped: false,
1482
+ reason: "metadata-not-found"
1483
+ };
1484
+ }
1485
+
1486
+ const stopResults = [];
1487
+ const watchdog = readJsonFile(getWatchdogPath(hostId));
1488
+ if (watchdog && Number.isInteger(watchdog.pid) && watchdog.pid !== process.pid) {
1489
+ stopResults.push({
1490
+ name: "watchdog",
1491
+ ...stopProcessTree(watchdog.pid)
1492
+ });
1493
+ }
1494
+ if (Number.isInteger(metadata.browserPid)) {
1495
+ stopResults.push({
1496
+ name: "headless-browser",
1497
+ ...stopProcessTree(metadata.browserPid)
1498
+ });
1499
+ }
1500
+ if (Number.isInteger(metadata.serveWebPid)) {
1501
+ stopResults.push({
1502
+ name: "serve-web",
1503
+ ...stopProcessTree(metadata.serveWebPid)
1504
+ });
1505
+ }
1506
+ if (typeof metadata.runDir === "string") {
1507
+ stopResults.push({
1508
+ name: "run-dir-processes",
1509
+ ...stopProcessesByCommandLineFragment(metadata.runDir)
1510
+ });
1511
+ }
1512
+
1513
+ let registryCleanup = {
1514
+ removed: false,
1515
+ path: metadata.registryPath ?? null
1516
+ };
1517
+ if (typeof metadata.registryPath === "string") {
1518
+ try {
1519
+ fs.rmSync(metadata.registryPath, { force: true });
1520
+ registryCleanup = {
1521
+ removed: true,
1522
+ path: metadata.registryPath
1523
+ };
1524
+ } catch (error) {
1525
+ registryCleanup = {
1526
+ removed: false,
1527
+ path: metadata.registryPath,
1528
+ error: error instanceof Error ? error.message : String(error)
1529
+ };
1530
+ }
1531
+ }
1532
+
1533
+ removeMetadata(hostId);
1534
+ fs.rmSync(getWatchdogPath(hostId), { force: true });
1535
+ return {
1536
+ ok: true,
1537
+ hostId,
1538
+ hostKind: metadata.hostKind ?? hostKind,
1539
+ stopped: true,
1540
+ metadata,
1541
+ stopResults,
1542
+ registryCleanup
1543
+ };
1544
+ }
1545
+
1546
+ async function stopAllManagedServeWebHosts(options = {}) {
1547
+ const entries = readAllManagedHostEntries();
1548
+ const stopped = [];
1549
+ for (const entry of entries) {
1550
+ const metadata = entry.metadata;
1551
+ const result = await stopManagedServeWebHost({
1552
+ ...options,
1553
+ all: false,
1554
+ hostId: entry.hostId,
1555
+ fixture: metadata.fixture ?? defaultFixtureDir,
1556
+ hostKind: metadata.hostKind
1557
+ });
1558
+ stopped.push({
1559
+ hostId: entry.hostId,
1560
+ hostKind: result.hostKind ?? metadata.hostKind ?? null,
1561
+ fixture: metadata.fixture ?? null,
1562
+ runDir: metadata.runDir ?? null,
1563
+ stopped: result.stopped === true,
1564
+ reason: result.reason ?? null
1565
+ });
1566
+ }
1567
+ return {
1568
+ ok: true,
1569
+ all: true,
1570
+ count: stopped.length,
1571
+ stopped: stopped.filter((item) => item.stopped === true).length,
1572
+ runtimes: stopped
1573
+ };
1574
+ }
1575
+
1576
+ async function killManagedServeWebHosts(options = {}) {
1577
+ if (options.all !== true) {
1578
+ return {
1579
+ ok: false,
1580
+ code: "RUNTIME_KILL_REQUIRES_ALL",
1581
+ error: "runtime kill currently requires --all."
1582
+ };
1583
+ }
1584
+ const stopResult = await stopAllManagedServeWebHosts(options);
1585
+ const orphanCleanup = stopAibOwnedRuntimeProcesses();
1586
+ return {
1587
+ ok: true,
1588
+ all: true,
1589
+ stop: stopResult,
1590
+ killed: orphanCleanup.killed ?? 0,
1591
+ orphanCleanup
1592
+ };
1593
+ }
1594
+
1595
+ async function cleanupExpiredManagedServeWebHosts(options = {}) {
1596
+ const ttlMs = readTtlMs(options.ttlMs);
1597
+ if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
1598
+ return {
1599
+ ok: true,
1600
+ ttlMs,
1601
+ stopped: []
1602
+ };
1603
+ }
1604
+ if (!fs.existsSync(managedRootDir)) {
1605
+ return {
1606
+ ok: true,
1607
+ ttlMs,
1608
+ stopped: []
1609
+ };
1610
+ }
1611
+
1612
+ const stopped = [];
1613
+ const activeRunDirs = [];
1614
+ for (const item of fs.readdirSync(managedRootDir, { withFileTypes: true })) {
1615
+ if (!item.isDirectory()) {
1616
+ continue;
1617
+ }
1618
+ const hostId = item.name;
1619
+ const metadata = readMetadataByHostId(hostId);
1620
+ if (!metadata || !isMetadataExpired(metadata, ttlMs)) {
1621
+ if (metadata && typeof metadata.runDir === "string" && metadata.runDir.length > 0) {
1622
+ activeRunDirs.push(metadata.runDir);
1623
+ }
1624
+ continue;
1625
+ }
1626
+ const result = await stopManagedServeWebHost({ hostId, fixture: metadata.fixture ?? defaultFixtureDir });
1627
+ stopped.push({
1628
+ hostId,
1629
+ fixture: metadata.fixture ?? null,
1630
+ lastActivityAt: metadata.lastActivityAt ?? metadata.updatedAt ?? null,
1631
+ stopped: result.stopped === true
1632
+ });
1633
+ }
1634
+ const orphanCleanup = stopOrphanedPackagedRuntimeProcesses(activeRunDirs);
1635
+
1636
+ return {
1637
+ ok: true,
1638
+ ttlMs,
1639
+ stopped,
1640
+ orphanCleanup
1641
+ };
1642
+ }
1643
+
1644
+ async function runManagedServeWebWatchdog(options = {}) {
1645
+ const fixture = path.resolve(options.fixture ?? defaultFixtureDir);
1646
+ const codeCommand = resolveManagedIdeCommand(options);
1647
+ const hostKind = resolveManagedHostKind(options, codeCommand);
1648
+ const hostId = options.hostId ?? buildHostId({ fixture, hostKind });
1649
+ const ttlMs = readTtlMs(options.ttlMs);
1650
+ const intervalMs = Number.isFinite(Number(options.intervalMs)) && Number(options.intervalMs) > 0
1651
+ ? Number(options.intervalMs)
1652
+ : 30_000;
1653
+ if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
1654
+ return {
1655
+ ok: true,
1656
+ hostId,
1657
+ hostKind,
1658
+ watching: false,
1659
+ reason: "ttl-disabled"
1660
+ };
1661
+ }
1662
+
1663
+ writeJsonAtomic(getWatchdogPath(hostId), {
1664
+ pid: process.pid,
1665
+ hostId,
1666
+ hostKind,
1667
+ fixture,
1668
+ ttlMs,
1669
+ intervalMs,
1670
+ startedAt: new Date().toISOString()
1671
+ });
1672
+
1673
+ while (true) {
1674
+ const metadata = readMetadataByHostId(hostId);
1675
+ if (!metadata) {
1676
+ return {
1677
+ ok: true,
1678
+ hostId,
1679
+ hostKind,
1680
+ stopped: false,
1681
+ reason: "metadata-not-found"
1682
+ };
1683
+ }
1684
+ const status = await getManagedServeWebHostStatus({ fixture, hostId, vsix: metadata.vsix, hostKind: metadata.hostKind ?? hostKind });
1685
+ if (!status.running) {
1686
+ return {
1687
+ ok: true,
1688
+ hostId,
1689
+ hostKind: metadata.hostKind ?? hostKind,
1690
+ stopped: false,
1691
+ reason: "not-running"
1692
+ };
1693
+ }
1694
+ if (isMetadataExpired(metadata, ttlMs)) {
1695
+ const result = await stopManagedServeWebHost({ fixture, hostId });
1696
+ return {
1697
+ ok: true,
1698
+ hostId,
1699
+ hostKind: metadata.hostKind ?? hostKind,
1700
+ stopped: result.stopped === true,
1701
+ reason: "ttl-expired"
1702
+ };
1703
+ }
1704
+ await sleep(intervalMs);
1705
+ }
1706
+ }
1707
+
1708
+ function startManagedServeWebWatchdog(options) {
1709
+ const ttlMs = readTtlMs(options.ttlMs);
1710
+ if (!Number.isFinite(ttlMs) || ttlMs <= 0) {
1711
+ return;
1712
+ }
1713
+ const existing = readJsonFile(getWatchdogPath(options.hostId));
1714
+ if (existing && isProcessAlive(existing.pid)) {
1715
+ return;
1716
+ }
1717
+ const stdoutPath = path.join(options.logsDir, "watchdog.stdout.txt");
1718
+ const stderrPath = path.join(options.logsDir, "watchdog.stderr.txt");
1719
+ const nodeRuntime = resolvePackagedNodeRuntime();
1720
+ startLoggedProcess(nodeRuntime.command, [
1721
+ path.join(__dirname, "manage-serve-web-host.cjs"),
1722
+ "watchdog",
1723
+ "--fixture",
1724
+ options.fixture,
1725
+ ...(options.ideCommand ? ["--ide-command", options.ideCommand] : []),
1726
+ ...(options.hostKind ? ["--host-kind", options.hostKind] : []),
1727
+ "--ttl-ms",
1728
+ String(ttlMs),
1729
+ "--interval-ms",
1730
+ String(options.intervalMs ?? 30_000)
1731
+ ], {
1732
+ cwd: rootDir,
1733
+ detached: true,
1734
+ stdoutPath,
1735
+ stderrPath
1736
+ });
1737
+ }
1738
+
1739
+ function buildStartSummary(input) {
1740
+ const codeCommand = input.codeCommand && typeof input.codeCommand === "object"
1741
+ ? commandDisplayName(input.codeCommand)
1742
+ : input.codeCommand;
1743
+ const codeCommandSource = input.codeCommandSource
1744
+ ?? (input.codeCommand && typeof input.codeCommand === "object" ? input.codeCommand.source : null);
1745
+ const summaryPath = input.reused
1746
+ ? path.join(input.runDir, `reuse-${timestampForPath()}.json`)
1747
+ : path.join(input.runDir, "summary.json");
1748
+ const summary = {
1749
+ ok: input.ok,
1750
+ reused: input.reused,
1751
+ hostId: input.hostId,
1752
+ kind: "serve-web-headless-client",
1753
+ startedAt: new Date(input.startedAt).toISOString(),
1754
+ finishedAt: new Date().toISOString(),
1755
+ durationMs: Date.now() - input.startedAt,
1756
+ fixture: input.fixture,
1757
+ vsix: input.vsix,
1758
+ vsixFingerprint: input.metadata?.vsixFingerprint ?? fingerprintFile(input.vsix),
1759
+ codeCommand,
1760
+ codeCommandSource,
1761
+ hostKind: input.hostKind ?? input.metadata?.hostKind ?? "vscode-serve-web",
1762
+ runDir: input.runDir,
1763
+ metadata: input.metadata,
1764
+ registryEntry: input.registryEntry ?? null,
1765
+ artifacts: {
1766
+ logsDir: input.logsDir,
1767
+ stepsDir: input.stepsDir,
1768
+ summaryPath
1769
+ },
1770
+ steps: input.steps.map((record) => ({
1771
+ name: record.name,
1772
+ ok: record.ok,
1773
+ durationMs: record.durationMs,
1774
+ path: record.path
1775
+ })),
1776
+ failures: input.failures
1777
+ };
1778
+ writeJson(summaryPath, summary);
1779
+ return summary;
1780
+ }
1781
+
1782
+ module.exports = {
1783
+ buildHostId,
1784
+ cleanupExpiredManagedServeWebHosts,
1785
+ getManagedServeWebHostStatus,
1786
+ killManagedServeWebHosts,
1787
+ runManagedServeWebWatchdog,
1788
+ startManagedServeWebHost,
1789
+ stopManagedServeWebHost
1790
+ };