@amd-gaia/agent-ui 0.27.0-rc.2 → 0.27.0-rc.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/gaia-ui.cjs +57 -3
- package/dist/assets/AdvancedSettings-DD-RVnWp.js +1 -0
- package/dist/assets/AutomationsSettings-rLo6mWU6.js +1 -0
- package/dist/assets/ConnectorsPane-BxG6qzLb.css +1 -0
- package/dist/assets/ConnectorsPane-DdYiHRhD.js +1 -0
- package/dist/assets/DocumentLibrary-BY3B6vwk.js +5 -0
- package/dist/assets/{DocumentLibrary-fEMoY7lH.css → DocumentLibrary-Ctwq2m2t.css} +1 -1
- package/dist/assets/FileBrowser-DZVXejEq.js +4 -0
- package/dist/assets/FileBrowser-DxS29ShT.css +1 -0
- package/dist/assets/MemoryDashboard-Bfy9g6TS.css +1 -0
- package/dist/assets/MemoryDashboard-BlNGTH6-.js +1 -0
- package/dist/assets/MessagingSettings-BHd7ftWx.js +1 -0
- package/dist/assets/{MobileAccessModal-BrZMaERi.css → MobileAccessModal-DSFjH6di.css} +1 -1
- package/dist/assets/MobileAccessModal-DbSJ06l7.js +2 -0
- package/dist/assets/ScheduleManager-CEBoBLPV.css +1 -0
- package/dist/assets/ScheduleManager-ClE3JXAN.js +1 -0
- package/dist/assets/folderIndexResult-BIoxqF5I.js +1 -0
- package/dist/assets/index-DDO2kO5x.js +226 -0
- package/dist/assets/index-DXyVxzzC.css +1 -0
- package/dist/assets/useFocusTrap-Cy_PGcml.js +6 -0
- package/dist/index.html +3 -3
- package/dist/sw.js +1 -1
- package/main-safety-net.cjs +26 -2
- package/main.cjs +226 -122
- package/package.json +2 -2
- package/preload.cjs +6 -0
- package/services/auto-updater.cjs +99 -0
- package/services/backend-crash-dialog.cjs +19 -5
- package/services/backend-health-monitor.cjs +86 -0
- package/services/backend-installer.cjs +1 -0
- package/services/backend-orphan.cjs +162 -1
- package/services/backend-supervisor.cjs +35 -9
- package/services/browser-pane/dev-servers.cjs +44 -1
- package/services/browser-pane/listen-addresses.cjs +140 -0
- package/services/context-menu.cjs +79 -0
- package/services/link-policy.cjs +78 -7
- package/services/port-manager.cjs +2 -3
- package/services/process-tree.cjs +84 -14
- package/services/quit-guard.cjs +8 -4
- package/services/ui-token.cjs +156 -0
- package/dist/assets/AdvancedSettings-DHERtrno.js +0 -1
- package/dist/assets/AutomationsSettings-4FWcRau9.js +0 -1
- package/dist/assets/ConnectorsPane-BGI8nBUb.css +0 -1
- package/dist/assets/ConnectorsPane-Dkat1zLC.js +0 -1
- package/dist/assets/DocumentLibrary-Dll55N7l.js +0 -5
- package/dist/assets/FileBrowser-1goNp4CP.css +0 -1
- package/dist/assets/FileBrowser-wFHqH6K_.js +0 -4
- package/dist/assets/MemoryDashboard-BuUBoNq8.js +0 -1
- package/dist/assets/MemoryDashboard-RpfLag-v.css +0 -1
- package/dist/assets/MessagingSettings-DomiT2rX.js +0 -1
- package/dist/assets/MobileAccessModal-B2gaE8nk.js +0 -2
- package/dist/assets/ScheduleManager-8UE81KlQ.js +0 -1
- package/dist/assets/ScheduleManager-BZzEH2-W.css +0 -1
- package/dist/assets/index-BiETDNik.css +0 -1
- package/dist/assets/index-HCZ86cyP.js +0 -220
- package/dist/assets/useFocusTrap-BFi0chLf.js +0 -6
- package/dist/assets/zap-DjkIffnX.js +0 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright(C) 2026 Advanced Micro Devices, Inc. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The local addresses TCP listeners on one port are bound to, read from the
|
|
6
|
+
* OS: `lsof` on macOS and other Unixes, /proc/net/tcp{,6} on Linux, and
|
|
7
|
+
* `netstat -ano` on Windows. A dev server GAIA starts must listen only on
|
|
8
|
+
* loopback, so nothing else on the network can reach the folder it serves.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
"use strict";
|
|
12
|
+
|
|
13
|
+
const childProcess = require("child_process");
|
|
14
|
+
const fs = require("fs");
|
|
15
|
+
const os = require("os");
|
|
16
|
+
const path = require("path");
|
|
17
|
+
|
|
18
|
+
const LIST_TIMEOUT_MS = 10000;
|
|
19
|
+
const PROC_LISTEN_STATE = "0A";
|
|
20
|
+
|
|
21
|
+
/** True for 127.0.0.0/8 and ::1 (also as an IPv4-mapped address). "*", 0.0.0.0 and :: are every interface. */
|
|
22
|
+
function isLoopback(address) {
|
|
23
|
+
const a = String(address).replace(/^\[|\]$/g, "").replace(/%.*$/, "").toLowerCase();
|
|
24
|
+
if (/^127\.\d+\.\d+\.\d+$/.test(a)) return true;
|
|
25
|
+
if (/^::ffff:127\.\d+\.\d+\.\d+$/.test(a)) return true;
|
|
26
|
+
return a === "::1" || a === "0:0:0:0:0:0:0:1";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** "1.2.3.4:80" or "[::1]:80" -> { address, port }; null when there is no port. */
|
|
30
|
+
function splitHostPort(text) {
|
|
31
|
+
const cut = text.lastIndexOf(":");
|
|
32
|
+
if (cut <= 0) return null;
|
|
33
|
+
const port = Number(text.slice(cut + 1));
|
|
34
|
+
if (!Number.isInteger(port)) return null;
|
|
35
|
+
return { address: text.slice(0, cut).replace(/^\[|\]$/g, ""), port };
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `lsof -nP -iTCP:<port> -sTCP:LISTEN -Fn` output: one "n<addr>:<port>" line per socket. */
|
|
39
|
+
function parseLsof(stdout, port) {
|
|
40
|
+
const found = [];
|
|
41
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
42
|
+
if (!line.startsWith("n")) continue;
|
|
43
|
+
const hp = splitHostPort(line.slice(1));
|
|
44
|
+
if (hp && hp.port === port) found.push(hp.address);
|
|
45
|
+
}
|
|
46
|
+
return found;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** A /proc/net/tcp address: 32-bit words in host byte order, hex. */
|
|
50
|
+
function decodeProcAddress(hex, endianness = os.endianness()) {
|
|
51
|
+
const bytes = [];
|
|
52
|
+
for (let w = 0; w < hex.length; w += 8) {
|
|
53
|
+
const word = hex.slice(w, w + 8).match(/../g).map((b) => parseInt(b, 16));
|
|
54
|
+
bytes.push(...(endianness === "LE" ? word.reverse() : word));
|
|
55
|
+
}
|
|
56
|
+
if (bytes.length === 4) return bytes.join(".");
|
|
57
|
+
if (bytes.every((b) => b === 0)) return "::";
|
|
58
|
+
if (bytes.slice(0, 15).every((b) => b === 0) && bytes[15] === 1) return "::1";
|
|
59
|
+
if (bytes.slice(0, 10).every((b) => b === 0) && bytes[10] === 0xff && bytes[11] === 0xff) {
|
|
60
|
+
return `::ffff:${bytes.slice(12).join(".")}`;
|
|
61
|
+
}
|
|
62
|
+
const groups = [];
|
|
63
|
+
for (let i = 0; i < 16; i += 2) groups.push(((bytes[i] << 8) | bytes[i + 1]).toString(16));
|
|
64
|
+
return groups.join(":");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The listening sockets on `port` in one /proc/net/tcp or tcp6 table. */
|
|
68
|
+
function parseProcNetTcp(text, port, endianness = os.endianness()) {
|
|
69
|
+
const found = [];
|
|
70
|
+
for (const line of text.split(/\r?\n/).slice(1)) {
|
|
71
|
+
const cols = line.trim().split(/\s+/);
|
|
72
|
+
if (cols.length < 4 || cols[3] !== PROC_LISTEN_STATE) continue;
|
|
73
|
+
const [addrHex, portHex] = cols[1].split(":");
|
|
74
|
+
if (parseInt(portHex, 16) !== port) continue;
|
|
75
|
+
found.push(decodeProcAddress(addrHex, endianness));
|
|
76
|
+
}
|
|
77
|
+
return found;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* `netstat -ano` output. The state column is translated on non-English
|
|
82
|
+
* Windows, so a listener is told by its foreign address, which is always
|
|
83
|
+
* 0.0.0.0:0 or [::]:0 for one and never for a connection.
|
|
84
|
+
*/
|
|
85
|
+
function parseNetstat(stdout, port) {
|
|
86
|
+
const found = [];
|
|
87
|
+
for (const line of stdout.split(/\r?\n/)) {
|
|
88
|
+
const cols = line.trim().split(/\s+/);
|
|
89
|
+
if (cols.length < 4 || cols[0].toUpperCase() !== "TCP") continue;
|
|
90
|
+
if (cols[2] !== "0.0.0.0:0" && cols[2] !== "[::]:0") continue;
|
|
91
|
+
const hp = splitHostPort(cols[1]);
|
|
92
|
+
if (hp && hp.port === port) found.push(hp.address);
|
|
93
|
+
}
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function run(execFile, file, args) {
|
|
98
|
+
return new Promise((resolve, reject) => {
|
|
99
|
+
execFile(file, args, { timeout: LIST_TIMEOUT_MS, windowsHide: true, maxBuffer: 16 * 1024 * 1024 }, (err, stdout) => {
|
|
100
|
+
if (err) reject(Object.assign(err, { stdout: String(stdout || "") }));
|
|
101
|
+
else resolve(String(stdout));
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Every local address something listens on at `port`. Rejects when the OS
|
|
108
|
+
* can't be asked, never answers "none" for a list it could not read.
|
|
109
|
+
* @param {number} port
|
|
110
|
+
* @returns {Promise<string[]>}
|
|
111
|
+
*/
|
|
112
|
+
async function listeningAddresses(
|
|
113
|
+
port,
|
|
114
|
+
{ platform = process.platform, execFile = childProcess.execFile, readFile = fs.promises.readFile, env = process.env } = {}
|
|
115
|
+
) {
|
|
116
|
+
if (platform === "linux") {
|
|
117
|
+
const v4 = await readFile("/proc/net/tcp", "utf8");
|
|
118
|
+
let v6 = "";
|
|
119
|
+
try {
|
|
120
|
+
v6 = await readFile("/proc/net/tcp6", "utf8");
|
|
121
|
+
} catch (err) {
|
|
122
|
+
if (err.code !== "ENOENT") throw err;
|
|
123
|
+
}
|
|
124
|
+
return [...parseProcNetTcp(v4, port), ...parseProcNetTcp(v6, port)];
|
|
125
|
+
}
|
|
126
|
+
if (platform === "win32") {
|
|
127
|
+
const root = env.SystemRoot || env.SYSTEMROOT || "C:\\Windows";
|
|
128
|
+
return parseNetstat(await run(execFile, path.win32.join(root, "System32", "netstat.exe"), ["-ano"]), port);
|
|
129
|
+
}
|
|
130
|
+
const lsof = platform === "darwin" ? "/usr/sbin/lsof" : "lsof";
|
|
131
|
+
try {
|
|
132
|
+
return parseLsof(await run(execFile, lsof, ["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-Fn"]), port);
|
|
133
|
+
} catch (err) {
|
|
134
|
+
// lsof exits 1 with no output when nothing matches.
|
|
135
|
+
if (err.code === 1 && !err.stdout.trim()) return [];
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
module.exports = { decodeProcAddress, isLoopback, listeningAddresses, parseLsof, parseNetstat, parseProcNetTcp };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// Copyright(C) 2026 Advanced Micro Devices, Inc. All rights reserved.
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Right-click menu for the app window. Electron shows none by default, and on
|
|
6
|
+
* Windows and Linux the window has no Edit menu either. Role items take their
|
|
7
|
+
* labels from the OS, so the rest follow the OS language too.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const MAX_SUGGESTIONS = 5;
|
|
11
|
+
|
|
12
|
+
const LABELS = {
|
|
13
|
+
en: { addToDictionary: "Add to dictionary", copyLink: "Copy link address", copyImage: "Copy image" },
|
|
14
|
+
ar: { addToDictionary: "إضافة إلى القاموس", copyLink: "نسخ عنوان الرابط", copyImage: "نسخ الصورة" },
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** The custom items' labels for an OS locale such as "ar-EG"; English for any language without them. */
|
|
18
|
+
function menuLabels(locale) {
|
|
19
|
+
const lang = String(locale || "").toLowerCase().split(/[-_]/)[0];
|
|
20
|
+
return LABELS[lang] || LABELS.en;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The menu template for one right-click, or [] when there is nothing to offer.
|
|
25
|
+
* `params` is Electron's context-menu ContextMenuParams.
|
|
26
|
+
*/
|
|
27
|
+
function buildContextMenuTemplate(params, actions, labels = LABELS.en) {
|
|
28
|
+
const sections = [];
|
|
29
|
+
|
|
30
|
+
if (params.isEditable) {
|
|
31
|
+
if (params.misspelledWord) {
|
|
32
|
+
const spelling = params.dictionarySuggestions
|
|
33
|
+
.slice(0, MAX_SUGGESTIONS)
|
|
34
|
+
.map((word) => ({ label: word, click: () => actions.replaceMisspelling(word) }));
|
|
35
|
+
spelling.push({
|
|
36
|
+
label: labels.addToDictionary,
|
|
37
|
+
click: () => actions.addToDictionary(params.misspelledWord),
|
|
38
|
+
});
|
|
39
|
+
sections.push(spelling);
|
|
40
|
+
}
|
|
41
|
+
const flags = params.editFlags;
|
|
42
|
+
sections.push([
|
|
43
|
+
{ role: "cut", enabled: flags.canCut },
|
|
44
|
+
{ role: "copy", enabled: flags.canCopy },
|
|
45
|
+
{ role: "paste", enabled: flags.canPaste },
|
|
46
|
+
{ type: "separator" },
|
|
47
|
+
{ role: "selectAll", enabled: flags.canSelectAll },
|
|
48
|
+
]);
|
|
49
|
+
} else if (params.selectionText && params.selectionText.trim()) {
|
|
50
|
+
sections.push([{ role: "copy" }]);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (params.linkURL) {
|
|
54
|
+
sections.push([{ label: labels.copyLink, click: () => actions.copyLink(params.linkURL) }]);
|
|
55
|
+
}
|
|
56
|
+
if (params.mediaType === "image" && params.hasImageContents) {
|
|
57
|
+
sections.push([{ label: labels.copyImage, click: () => actions.copyImageAt(params.x, params.y) }]);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return sections.flatMap((items, i) => (i === 0 ? items : [{ type: "separator" }, ...items]));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** Show the menu on right-click in `window`'s own webContents. `locale` is the OS's, as `app.getLocale()` gives it. */
|
|
64
|
+
function installContextMenu(window, { Menu, clipboard, locale }) {
|
|
65
|
+
const wc = window.webContents;
|
|
66
|
+
const labels = menuLabels(locale);
|
|
67
|
+
wc.on("context-menu", (_event, params) => {
|
|
68
|
+
const template = buildContextMenuTemplate(params, {
|
|
69
|
+
replaceMisspelling: (word) => wc.replaceMisspelling(word),
|
|
70
|
+
addToDictionary: (word) => wc.session.addWordToSpellCheckerDictionary(word),
|
|
71
|
+
copyLink: (url) => clipboard.writeText(url),
|
|
72
|
+
copyImageAt: (x, y) => wc.copyImageAt(x, y),
|
|
73
|
+
}, labels);
|
|
74
|
+
if (template.length === 0) return;
|
|
75
|
+
Menu.buildFromTemplate(template).popup({ window });
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
module.exports = { buildContextMenuTemplate, installContextMenu, menuLabels, MAX_SUGGESTIONS };
|
package/services/link-policy.cjs
CHANGED
|
@@ -60,26 +60,94 @@ function isInAppNavigation(targetUrl, currentUrl) {
|
|
|
60
60
|
);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
+
// U+2068/U+2069 keep a URL left-to-right inside Arabic text.
|
|
64
|
+
const isolate = (text) => `${text}`;
|
|
65
|
+
|
|
66
|
+
const NOTICE_TEXT = {
|
|
67
|
+
en: {
|
|
68
|
+
blockedTitle: "Link blocked",
|
|
69
|
+
notOpenedTitle: "Link not opened",
|
|
70
|
+
ok: "OK",
|
|
71
|
+
noScheme: "no scheme",
|
|
72
|
+
blocked: (url, scheme) =>
|
|
73
|
+
`GAIA refused to open "${url}" (${scheme}). ` +
|
|
74
|
+
"Only http, https and mailto links open outside the app — a link that " +
|
|
75
|
+
"resolves to a local file or network share is never launched.",
|
|
76
|
+
openFailed: (url, reason) =>
|
|
77
|
+
`GAIA couldn't open "${url}": ${reason}. ` +
|
|
78
|
+
"Check that this PC has an app set up for that kind of link.",
|
|
79
|
+
},
|
|
80
|
+
ar: {
|
|
81
|
+
blockedTitle: "تم حظر الرابط",
|
|
82
|
+
notOpenedTitle: "لم يُفتح الرابط",
|
|
83
|
+
ok: "حسنًا",
|
|
84
|
+
noScheme: "بلا بروتوكول",
|
|
85
|
+
blocked: (url, scheme) =>
|
|
86
|
+
`رفض GAIA فتح "${isolate(url)}" (${isolate(scheme)}). ` +
|
|
87
|
+
"لا تُفتح خارج التطبيق إلا روابط http وhttps وmailto، ولا يُشغَّل أبدًا " +
|
|
88
|
+
"رابط يشير إلى ملف محلي أو مجلد مشترك على الشبكة.",
|
|
89
|
+
openFailed: (url, reason) =>
|
|
90
|
+
`تعذّر على GAIA فتح "${isolate(url)}": ${isolate(reason)}. ` +
|
|
91
|
+
"تأكد من وجود تطبيق على هذا الجهاز مُعدّ لفتح هذا النوع من الروابط.",
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/** The link-notice strings for an OS locale such as "ar-EG"; English for any language without them. */
|
|
96
|
+
function linkNoticeText(locale) {
|
|
97
|
+
const lang = String(locale || "").toLowerCase().split(/[-_]/)[0];
|
|
98
|
+
return NOTICE_TEXT[lang] || NOTICE_TEXT.en;
|
|
99
|
+
}
|
|
100
|
+
|
|
63
101
|
/**
|
|
64
102
|
* A one-line, user-facing reason a URL was refused. Never silently dropped —
|
|
65
103
|
* the caller logs this and shows it, so a blocked click is explainable.
|
|
66
104
|
*
|
|
67
105
|
* @param {string} rawUrl
|
|
106
|
+
* @param {string} [locale] The OS locale, as `app.getLocale()` gives it.
|
|
68
107
|
* @returns {string}
|
|
69
108
|
*/
|
|
70
|
-
function describeBlockedUrl(rawUrl) {
|
|
109
|
+
function describeBlockedUrl(rawUrl, locale) {
|
|
110
|
+
const text = linkNoticeText(locale);
|
|
71
111
|
const shown = typeof rawUrl === "string" && rawUrl ? rawUrl : String(rawUrl);
|
|
72
|
-
let scheme =
|
|
112
|
+
let scheme = text.noScheme;
|
|
73
113
|
try {
|
|
74
114
|
scheme = new URL(shown).protocol.replace(/:$/, "");
|
|
75
115
|
} catch {
|
|
76
|
-
/* keep "no scheme" */
|
|
116
|
+
/* keep the "no scheme" label */
|
|
77
117
|
}
|
|
78
|
-
return (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
118
|
+
return text.blocked(shown.slice(0, 300), scheme);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* A one-line, user-facing reason the OS could not open an allowed link.
|
|
123
|
+
*
|
|
124
|
+
* @param {string} rawUrl
|
|
125
|
+
* @param {unknown} err
|
|
126
|
+
* @param {string} [locale] The OS locale, as `app.getLocale()` gives it.
|
|
127
|
+
* @returns {string}
|
|
128
|
+
*/
|
|
129
|
+
function describeOpenFailure(rawUrl, err, locale) {
|
|
130
|
+
const reason = (err && err.message ? err.message : String(err)).replace(
|
|
131
|
+
/\.+$/,
|
|
132
|
+
"",
|
|
82
133
|
);
|
|
134
|
+
return linkNoticeText(locale).openFailed(String(rawUrl).slice(0, 300), reason);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* `shell.openExternal` rejects when the OS has no handler (a mailto: link on a
|
|
139
|
+
* PC with no mail app). Unhandled, that rejection reaches the process-level
|
|
140
|
+
* fatal handler and quits the app, so every caller goes through here.
|
|
141
|
+
*
|
|
142
|
+
* @param {{ openExternal: (url: string) => Promise<void> }} shell
|
|
143
|
+
* @param {string} url
|
|
144
|
+
* @param {(err: unknown) => void} onFailure
|
|
145
|
+
* @returns {Promise<void>}
|
|
146
|
+
*/
|
|
147
|
+
function openExternalSafely(shell, url, onFailure) {
|
|
148
|
+
return Promise.resolve()
|
|
149
|
+
.then(() => shell.openExternal(url))
|
|
150
|
+
.catch(onFailure);
|
|
83
151
|
}
|
|
84
152
|
|
|
85
153
|
module.exports = {
|
|
@@ -87,4 +155,7 @@ module.exports = {
|
|
|
87
155
|
isAllowedExternalUrl,
|
|
88
156
|
isInAppNavigation,
|
|
89
157
|
describeBlockedUrl,
|
|
158
|
+
describeOpenFailure,
|
|
159
|
+
openExternalSafely,
|
|
160
|
+
linkNoticeText,
|
|
90
161
|
};
|
|
@@ -68,13 +68,12 @@ function findFreePort() {
|
|
|
68
68
|
async function killBackend(procOrPid, logger) {
|
|
69
69
|
const log = (logger && logger.log) || console.log;
|
|
70
70
|
const logError = (logger && logger.error) || console.error;
|
|
71
|
-
if (!procOrPid) return;
|
|
71
|
+
if (!procOrPid) return { killed: [], survivors: [], verified: true };
|
|
72
72
|
const proc = typeof procOrPid === "object" ? procOrPid : null;
|
|
73
73
|
if (proc && (proc.exitCode !== null || proc.signalCode !== null)) {
|
|
74
74
|
log(`[port-manager] child pid ${proc.pid} already exited (exitCode=${proc.exitCode} signal=${proc.signalCode})`);
|
|
75
|
-
return;
|
|
76
75
|
}
|
|
77
|
-
|
|
76
|
+
return killProcessTree(procOrPid, {
|
|
78
77
|
graceMs: BACKEND_KILL_GRACE_MS,
|
|
79
78
|
forceWaitMs: BACKEND_FORCE_WAIT_MS,
|
|
80
79
|
log,
|
|
@@ -22,6 +22,7 @@ const childProcess = require("child_process");
|
|
|
22
22
|
|
|
23
23
|
const DEFAULT_GRACE_MS = 3000;
|
|
24
24
|
const DEFAULT_FORCE_WAIT_MS = 1000;
|
|
25
|
+
const pendingTreeState = new WeakMap();
|
|
25
26
|
// PowerShell's CIM query takes about a second on an idle PC and several on a
|
|
26
27
|
// loaded one; timing out strands every process the agent started. `ps` is fast.
|
|
27
28
|
const WINDOWS_LIST_TIMEOUT_MS = 20000;
|
|
@@ -195,7 +196,7 @@ function rootAlive(proc, pid) {
|
|
|
195
196
|
* on Linux and macOS when the root may already have exited: its children
|
|
196
197
|
* are then re-parented and can no longer be found from it. Each is killed
|
|
197
198
|
* only while its command line still matches, so a reused pid is spared.
|
|
198
|
-
* @returns {Promise<{ killed: number[], survivors: number[] }>}
|
|
199
|
+
* @returns {Promise<{ killed: number[], survivors: number[], verified: boolean }>}
|
|
199
200
|
*/
|
|
200
201
|
async function killProcessTree(target, opts = {}) {
|
|
201
202
|
const {
|
|
@@ -209,36 +210,75 @@ async function killProcessTree(target, opts = {}) {
|
|
|
209
210
|
} = opts;
|
|
210
211
|
const proc = target && typeof target === "object" ? target : null;
|
|
211
212
|
const pid = proc ? proc.pid : target;
|
|
212
|
-
if (!pid) return { killed: [], survivors: [] };
|
|
213
|
+
if (!pid) return { killed: [], survivors: [], verified: true };
|
|
213
214
|
|
|
214
215
|
const readTable = () => (opts.listProcesses || listProcesses)(opts);
|
|
215
216
|
|
|
216
217
|
// pid -> command, for every non-root member we intend to stop.
|
|
218
|
+
const priorState = proc ? pendingTreeState.get(proc) : null;
|
|
219
|
+
const known = new Map((priorState && priorState.descendants ? priorState.descendants : []).map((d) => [d.pid, d]));
|
|
220
|
+
for (const d of knownDescendants) known.set(d.pid, d);
|
|
221
|
+
let unknownTree = Boolean(priorState && priorState.unknown);
|
|
222
|
+
if (!rootAlive(proc, pid) && known.size === 0) unknownTree = true;
|
|
217
223
|
const members = new Map();
|
|
224
|
+
const unverifiedMembers = new Map();
|
|
218
225
|
const absorb = (table) => {
|
|
219
226
|
const live = new Map(table.map((r) => [r.pid, r.command]));
|
|
227
|
+
for (const [memberPid, descendant] of [...known]) {
|
|
228
|
+
const command = live.get(memberPid);
|
|
229
|
+
if (command === descendant.command) {
|
|
230
|
+
unverifiedMembers.delete(memberPid);
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
if (command !== undefined || !pidAlive(memberPid)) {
|
|
234
|
+
known.delete(memberPid);
|
|
235
|
+
members.delete(memberPid);
|
|
236
|
+
unverifiedMembers.delete(memberPid);
|
|
237
|
+
} else {
|
|
238
|
+
unverifiedMembers.set(memberPid, descendant);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
220
241
|
// A member that exited, or whose pid now runs something else, is no
|
|
221
242
|
// longer ours to signal.
|
|
222
243
|
for (const [memberPid, command] of [...members]) {
|
|
223
|
-
if (live.get(memberPid) !== command)
|
|
244
|
+
if (live.get(memberPid) !== command) {
|
|
245
|
+
members.delete(memberPid);
|
|
246
|
+
if (live.has(memberPid) || !pidAlive(memberPid)) known.delete(memberPid);
|
|
247
|
+
}
|
|
224
248
|
}
|
|
225
|
-
for (const d of
|
|
249
|
+
for (const d of known.values()) {
|
|
226
250
|
if (live.has(d.pid) && live.get(d.pid) === d.command) members.set(d.pid, d.command);
|
|
227
251
|
}
|
|
228
252
|
if (live.has(pid) && rootAlive(proc, pid)) {
|
|
229
|
-
for (const d of descendantsFromTable(table, pid).descendants)
|
|
253
|
+
for (const d of descendantsFromTable(table, pid).descendants) {
|
|
254
|
+
members.set(d.pid, d.command);
|
|
255
|
+
known.set(d.pid, d);
|
|
256
|
+
}
|
|
230
257
|
}
|
|
231
258
|
// Children of members that outlived their parent's handle.
|
|
232
259
|
for (const memberPid of [...members.keys()]) {
|
|
233
|
-
for (const d of descendantsFromTable(table, memberPid).descendants)
|
|
260
|
+
for (const d of descendantsFromTable(table, memberPid).descendants) {
|
|
261
|
+
members.set(d.pid, d.command);
|
|
262
|
+
known.set(d.pid, d);
|
|
263
|
+
}
|
|
234
264
|
}
|
|
235
265
|
};
|
|
236
266
|
|
|
237
267
|
let tableError = null;
|
|
238
268
|
try {
|
|
239
|
-
|
|
269
|
+
const table = await readTable();
|
|
270
|
+
if (rootAlive(proc, pid) && !table.some((row) => row.pid === pid)) {
|
|
271
|
+
tableError = new Error(`the live root pid ${pid} was missing from the process table`);
|
|
272
|
+
unknownTree = true;
|
|
273
|
+
logError(`[process-tree] ${label} ${pid}: ${tableError.message}; descendants may keep running.`);
|
|
274
|
+
}
|
|
275
|
+
absorb(table);
|
|
276
|
+
if (!rootAlive(proc, pid) && known.size === 0 && unverifiedMembers.size === 0) unknownTree = false;
|
|
240
277
|
} catch (err) {
|
|
241
278
|
tableError = err;
|
|
279
|
+
unknownTree = true;
|
|
280
|
+
for (const d of known.values()) unverifiedMembers.set(d.pid, d);
|
|
281
|
+
members.clear();
|
|
242
282
|
logError(
|
|
243
283
|
`[process-tree] ${label} ${pid}: ${err.message} — only the ${label} itself will be stopped; ` +
|
|
244
284
|
"any process it started may keep running. Check them with Task Manager or `ps`."
|
|
@@ -248,7 +288,8 @@ async function killProcessTree(target, opts = {}) {
|
|
|
248
288
|
const signal = (p, sig) => {
|
|
249
289
|
try {
|
|
250
290
|
if (p === pid && proc) {
|
|
251
|
-
if (rootAlive(proc, pid))
|
|
291
|
+
if (!rootAlive(proc, pid)) return false;
|
|
292
|
+
return proc.kill(sig) !== false;
|
|
252
293
|
} else {
|
|
253
294
|
process.kill(p, sig);
|
|
254
295
|
}
|
|
@@ -271,27 +312,56 @@ async function killProcessTree(target, opts = {}) {
|
|
|
271
312
|
};
|
|
272
313
|
|
|
273
314
|
const all = () => [pid, ...members.keys()];
|
|
315
|
+
const observed = () => [...new Set([...all(), ...unverifiedMembers.keys()])];
|
|
274
316
|
const killed = [];
|
|
275
317
|
for (const p of all()) {
|
|
276
318
|
if (alive(p) && signal(p, "SIGTERM")) killed.push(p);
|
|
277
319
|
}
|
|
278
|
-
if (killed.length
|
|
279
|
-
|
|
320
|
+
if (killed.length) {
|
|
321
|
+
log(`[process-tree] SIGTERM sent to ${label} ${pid} and ${killed.length - (killed.includes(pid) ? 1 : 0)} descendant(s)`);
|
|
322
|
+
}
|
|
280
323
|
|
|
281
|
-
|
|
324
|
+
const finish = (survivors, verified) => {
|
|
325
|
+
if (!rootAlive(proc, pid)) {
|
|
326
|
+
for (const memberPid of known.keys()) {
|
|
327
|
+
if (!pidAlive(memberPid)) known.delete(memberPid);
|
|
328
|
+
}
|
|
329
|
+
for (const memberPid of unverifiedMembers.keys()) {
|
|
330
|
+
if (!pidAlive(memberPid)) unverifiedMembers.delete(memberPid);
|
|
331
|
+
}
|
|
332
|
+
if (!tableError && known.size === 0 && unverifiedMembers.size === 0) unknownTree = false;
|
|
333
|
+
verified = !unknownTree && !tableError && survivors.length === 0;
|
|
334
|
+
}
|
|
335
|
+
const result = { killed, survivors: [...new Set(survivors)], verified };
|
|
336
|
+
if (proc) {
|
|
337
|
+
if (verified) pendingTreeState.delete(proc);
|
|
338
|
+
else pendingTreeState.set(proc, { descendants: [...known.values()], unknown: unknownTree });
|
|
339
|
+
}
|
|
340
|
+
return result;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
if (await waitAllGone(observed(), graceMs)) {
|
|
344
|
+
const survivors = [...unverifiedMembers.keys()].filter(pidAlive);
|
|
345
|
+
return finish(survivors, !unknownTree && !tableError && survivors.length === 0);
|
|
346
|
+
}
|
|
282
347
|
|
|
283
348
|
if (platform === "win32") {
|
|
284
349
|
// SIGTERM was already TerminateProcess, so a member still listed is one
|
|
285
350
|
// that could not be ended or a pid already reused: only the root, through
|
|
286
351
|
// its handle, is worth a second try. Skipping the re-read also keeps a
|
|
287
352
|
// slow PowerShell from eating the quit's time limit.
|
|
353
|
+
for (const [memberPid, command] of members) {
|
|
354
|
+
if (alive(memberPid)) unverifiedMembers.set(memberPid, { pid: memberPid, command });
|
|
355
|
+
}
|
|
288
356
|
members.clear();
|
|
289
357
|
} else if (!tableError) {
|
|
290
358
|
try {
|
|
291
359
|
absorb(await readTable());
|
|
292
360
|
} catch (err) {
|
|
293
361
|
// Without a fresh table a member's pid may already belong to a stranger.
|
|
362
|
+
for (const [memberPid, command] of members) unverifiedMembers.set(memberPid, { pid: memberPid, command });
|
|
294
363
|
members.clear();
|
|
364
|
+
unknownTree = true;
|
|
295
365
|
logError(
|
|
296
366
|
`[process-tree] ${label} ${pid}: re-reading the tree before SIGKILL failed (${err.message}); ` +
|
|
297
367
|
`only the ${label} itself gets SIGKILL — any process it started may keep running.`
|
|
@@ -304,15 +374,15 @@ async function killProcessTree(target, opts = {}) {
|
|
|
304
374
|
}
|
|
305
375
|
log(`[process-tree] SIGKILL sent to ${stubborn.length} process(es) of ${label} ${pid} that ignored SIGTERM`);
|
|
306
376
|
|
|
307
|
-
await waitAllGone(stubborn, forceWaitMs);
|
|
308
|
-
const survivors = stubborn.filter(alive);
|
|
377
|
+
await waitAllGone([...stubborn, ...unverifiedMembers.keys()], forceWaitMs);
|
|
378
|
+
const survivors = [...new Set([...stubborn.filter(alive), ...[...unverifiedMembers.keys()].filter(pidAlive)])];
|
|
309
379
|
if (survivors.length) {
|
|
310
380
|
logError(
|
|
311
381
|
`[process-tree] ${label} ${pid}: pid(s) ${survivors.join(", ")} still running ${forceWaitMs} ms after SIGKILL — ` +
|
|
312
382
|
"end them with Task Manager or `kill -9` before relaunching GAIA."
|
|
313
383
|
);
|
|
314
384
|
}
|
|
315
|
-
return
|
|
385
|
+
return finish(survivors, !unknownTree && !tableError && survivors.length === 0);
|
|
316
386
|
}
|
|
317
387
|
|
|
318
388
|
module.exports = {
|
package/services/quit-guard.cjs
CHANGED
|
@@ -9,6 +9,8 @@
|
|
|
9
9
|
* so nothing polls while GAIA is idle.
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
const { authHeaders, defaultReader } = require("./ui-token.cjs");
|
|
13
|
+
|
|
12
14
|
const CHECK_TIMEOUT_MS = 3000;
|
|
13
15
|
// After the quit question fails, how long a second quit goes through without it.
|
|
14
16
|
const QUIT_ANYWAY_MS = 60_000;
|
|
@@ -17,14 +19,14 @@ const QUIT_ANYWAY_MS = 60_000;
|
|
|
17
19
|
async function backendJson(
|
|
18
20
|
port,
|
|
19
21
|
route,
|
|
20
|
-
{ method = "GET", body, timeoutMs = CHECK_TIMEOUT_MS, fetchImpl = fetch } = {}
|
|
22
|
+
{ method = "GET", body, timeoutMs = CHECK_TIMEOUT_MS, fetchImpl = fetch, tokenReader = defaultReader } = {}
|
|
21
23
|
) {
|
|
22
24
|
const url = `http://127.0.0.1:${port}${route}`;
|
|
25
|
+
// The service refuses a request without its token, and a state-changing one without X-Gaia-UI (ui/security.py).
|
|
26
|
+
const headers = { "X-Gaia-UI": "1", ...authHeaders(tokenReader) };
|
|
27
|
+
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
23
28
|
let resp;
|
|
24
29
|
try {
|
|
25
|
-
const headers = { "X-Gaia-UI": "1" };
|
|
26
|
-
// The service refuses a state-changing request without X-Gaia-UI (ui/security.py).
|
|
27
|
-
if (body !== undefined) headers["Content-Type"] = "application/json";
|
|
28
30
|
resp = await fetchImpl(url, {
|
|
29
31
|
method,
|
|
30
32
|
headers,
|
|
@@ -41,6 +43,8 @@ async function backendJson(
|
|
|
41
43
|
throw new Error(`the GAIA service sent an unreadable reply from ${route} (HTTP ${resp.status}): ${err.message}`);
|
|
42
44
|
}
|
|
43
45
|
if (!resp.ok) {
|
|
46
|
+
// A rotated token file is re-read on the next call.
|
|
47
|
+
if (resp.status === 401) tokenReader.clear();
|
|
44
48
|
const detail = reply && typeof reply.detail === "string" ? reply.detail : `HTTP ${resp.status}`;
|
|
45
49
|
const error = new Error(detail);
|
|
46
50
|
error.status = resp.status;
|