@50arms/platform-mcp 0.1.464 → 0.1.466
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/dist/access-request.js +19 -6
- package/dist/agent-gate.js +19 -5
- package/dist/board-id.js +8 -2
- package/dist/checkout-docs-root.js +10 -2
- package/dist/clear-control.js +18 -4
- package/dist/compaction-control.js +30 -7
- package/dist/credential.js +10 -2
- package/dist/danxbot-api-examples.js +20 -3
- package/dist/design-screenshot.js +22 -5
- package/dist/digest-hold.js +14 -3
- package/dist/docs-tools.js +46 -12
- package/dist/durable-write.js +10 -2
- package/dist/forbidden-hint.js +13 -2
- package/dist/guarded-calls.js +6 -2
- package/dist/handlers.js +66 -14
- package/dist/heartbeat-reporter.js +8 -3
- package/dist/http-client.js +4 -2
- package/dist/index.js +242 -80
- package/dist/job-key.js +3 -1
- package/dist/json-body.js +3 -1
- package/dist/key-revoked-halt.js +2 -1
- package/dist/leftover-wire.js +17 -4
- package/dist/lifecycle.js +11 -3
- package/dist/listen.js +242 -64
- package/dist/listener-reminder.js +8 -2
- package/dist/machine-daemon/autostart.js +16 -4
- package/dist/machine-daemon/beat.js +18 -4
- package/dist/machine-daemon/cli.js +15 -4
- package/dist/machine-daemon/install.js +33 -9
- package/dist/machine-daemon/leftovers.js +134 -30
- package/dist/machine-daemon/state.js +9 -3
- package/dist/machine-daemon.js +531 -128
- package/dist/machine-registration.js +27 -6
- package/dist/machine-secret.js +8 -3
- package/dist/machine-toolkit.js +377 -81
- package/dist/mini-app-build.js +19 -5
- package/dist/mini-app-checkout.js +38 -10
- package/dist/mini-app-output.js +22 -5
- package/dist/mini-app-publish.js +34 -11
- package/dist/mini-app-restore.js +30 -9
- package/dist/one-line.js +3 -1
- package/dist/permission-request.js +14 -3
- package/dist/permission-watch.js +10 -3
- package/dist/plan-events-relay.js +96 -19
- package/dist/plan-events-tool.js +9 -2
- package/dist/plugin-publish.js +23 -6
- package/dist/plugin-screenshot.js +18 -6
- package/dist/restart-notice.js +47 -15
- package/dist/result-limit.js +6 -1
- package/dist/role-instruction-file.js +16 -4
- package/dist/route-board-scope.generated.js +1 -0
- package/dist/route-board-scope.js +6 -2
- package/dist/session-access.js +110 -32
- package/dist/session-connection.js +23 -7
- package/dist/session-control-file.js +1 -1
- package/dist/session-key.js +12 -4
- package/dist/session-options.js +6 -2
- package/dist/session-stats.js +2 -1
- package/dist/session-stream.js +81 -20
- package/dist/shutdown.js +3 -1
- package/dist/subagents-live.js +8 -2
- package/dist/terms-halt.js +4 -2
- package/dist/tool-access.js +17 -4
- package/dist/tool-surface.js +9 -2
- package/dist/transcript-lineage.js +5 -2
- package/dist/urgent-records.js +1 -0
- package/package.json +1 -1
|
@@ -3,7 +3,10 @@ import { readSessionConnection } from "./session-connection.js";
|
|
|
3
3
|
export const HEALTH_CACHE_WINDOW_MS = 30_000;
|
|
4
4
|
function isReminder(value) {
|
|
5
5
|
const r = value;
|
|
6
|
-
return typeof r === "object" &&
|
|
6
|
+
return (typeof r === "object" &&
|
|
7
|
+
r !== null &&
|
|
8
|
+
typeof r.key === "string" &&
|
|
9
|
+
typeof r.text === "string");
|
|
7
10
|
}
|
|
8
11
|
/** The reminders the health response carries, or a thrown Error naming why it cannot be read. */
|
|
9
12
|
function readReminders(result) {
|
|
@@ -104,7 +107,10 @@ export function createListenerReminder(deps) {
|
|
|
104
107
|
});
|
|
105
108
|
if (fresh.length === 0)
|
|
106
109
|
return result;
|
|
107
|
-
return {
|
|
110
|
+
return {
|
|
111
|
+
...result,
|
|
112
|
+
body: { ...body, reminders: [...(existing ?? []), ...fresh] },
|
|
113
|
+
};
|
|
108
114
|
},
|
|
109
115
|
invalidate() {
|
|
110
116
|
generation += 1;
|
|
@@ -23,7 +23,11 @@ import { join } from "node:path";
|
|
|
23
23
|
export const TASK_NAME = "danxbot-machine-daemon";
|
|
24
24
|
export const UNIT_NAME = "danxbot-machine-daemon.service";
|
|
25
25
|
function xmlEscape(value) {
|
|
26
|
-
return value
|
|
26
|
+
return value
|
|
27
|
+
.replace(/&/g, "&")
|
|
28
|
+
.replace(/</g, "<")
|
|
29
|
+
.replace(/>/g, ">")
|
|
30
|
+
.replace(/"/g, """);
|
|
27
31
|
}
|
|
28
32
|
/** The Task Scheduler definition: this user's logon plus a once-a-minute time trigger, `conhost --headless` running node on the bundle. */
|
|
29
33
|
export function buildWindowsTaskXml(args) {
|
|
@@ -75,7 +79,11 @@ WantedBy=default.target
|
|
|
75
79
|
}
|
|
76
80
|
/** Runs an OS command with no window and returns its stdout; a non-zero exit throws. */
|
|
77
81
|
export function runHidden(file, args) {
|
|
78
|
-
return execFileSync(file, args, {
|
|
82
|
+
return execFileSync(file, args, {
|
|
83
|
+
encoding: "utf8",
|
|
84
|
+
windowsHide: true,
|
|
85
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
86
|
+
});
|
|
79
87
|
}
|
|
80
88
|
export function productionAutostartDeps(bundlePath) {
|
|
81
89
|
return {
|
|
@@ -85,11 +93,15 @@ export function productionAutostartDeps(bundlePath) {
|
|
|
85
93
|
// node there and the task ends at once. The native realpath resolves the redirect.
|
|
86
94
|
nodePath: realpathSync.native(process.execPath),
|
|
87
95
|
bundlePath,
|
|
88
|
-
userId: process.env.USERDOMAIN && process.env.USERNAME
|
|
96
|
+
userId: process.env.USERDOMAIN && process.env.USERNAME
|
|
97
|
+
? `${process.env.USERDOMAIN}\\${process.env.USERNAME}`
|
|
98
|
+
: (process.env.USER ?? ""),
|
|
89
99
|
conhostPath: join(process.env.SystemRoot ?? "C:\\Windows", "System32", "conhost.exe"),
|
|
90
100
|
home: homedir(),
|
|
91
101
|
run: runHidden,
|
|
92
|
-
writeFile: (file, content, encoding) => writeFileSync(file, encoding === "utf16le" ? `${content}` : content, {
|
|
102
|
+
writeFile: (file, content, encoding) => writeFileSync(file, encoding === "utf16le" ? `${content}` : content, {
|
|
103
|
+
encoding,
|
|
104
|
+
}),
|
|
93
105
|
mkdir: (dir) => mkdirSync(dir, { recursive: true }),
|
|
94
106
|
remove: (file) => rmSync(file, { force: true }),
|
|
95
107
|
};
|
|
@@ -14,13 +14,24 @@ export async function postAsMachine(config, fetchFn, route, payload) {
|
|
|
14
14
|
try {
|
|
15
15
|
const res = await fetchFn(`${config.dashboard_url.replace(/\/+$/, "")}${route}`, {
|
|
16
16
|
method: "POST",
|
|
17
|
-
headers: {
|
|
17
|
+
headers: {
|
|
18
|
+
"Content-Type": "application/json",
|
|
19
|
+
Authorization: `Bearer ${config.bearer}`,
|
|
20
|
+
},
|
|
18
21
|
body: JSON.stringify({ machine_id: config.machine_id, ...payload }),
|
|
19
22
|
});
|
|
20
|
-
return {
|
|
23
|
+
return {
|
|
24
|
+
status: res.status,
|
|
25
|
+
error: res.status === 200 ? null : `HTTP ${res.status}`,
|
|
26
|
+
text: await res.text(),
|
|
27
|
+
};
|
|
21
28
|
}
|
|
22
29
|
catch (err) {
|
|
23
|
-
return {
|
|
30
|
+
return {
|
|
31
|
+
status: null,
|
|
32
|
+
error: err instanceof Error ? err.message : String(err),
|
|
33
|
+
text: "",
|
|
34
|
+
};
|
|
24
35
|
}
|
|
25
36
|
}
|
|
26
37
|
/** Sends one beat. Returns the HTTP status, or null when the request itself failed. */
|
|
@@ -48,7 +59,10 @@ export async function runBeatLoop(deps) {
|
|
|
48
59
|
}
|
|
49
60
|
catch (err) {
|
|
50
61
|
// A vitals probe that cannot read the machine is loud: it is recorded and the next tick retries it.
|
|
51
|
-
outcome = {
|
|
62
|
+
outcome = {
|
|
63
|
+
status: null,
|
|
64
|
+
error: `vitals: ${err instanceof Error ? err.message : String(err)}`,
|
|
65
|
+
};
|
|
52
66
|
}
|
|
53
67
|
const next = new Date(beatAt.getTime() + BEAT_INTERVAL_MS);
|
|
54
68
|
deps.writeStatus({
|
|
@@ -4,10 +4,14 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { dirname, join } from "node:path";
|
|
6
6
|
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { DEFAULT_DASHBOARD_URL, daemonStatusText, installDaemon, productionInstallDeps, uninstallDaemon } from "./install.js";
|
|
7
|
+
import { DEFAULT_DASHBOARD_URL, daemonStatusText, installDaemon, productionInstallDeps, uninstallDaemon, } from "./install.js";
|
|
8
8
|
import { productionAutostartDeps } from "./autostart.js";
|
|
9
9
|
import { daemonBundleDir, danxbotHome } from "./state.js";
|
|
10
|
-
export const MACHINE_DAEMON_SUBCOMMANDS = [
|
|
10
|
+
export const MACHINE_DAEMON_SUBCOMMANDS = [
|
|
11
|
+
"install-daemon",
|
|
12
|
+
"status",
|
|
13
|
+
"uninstall-daemon",
|
|
14
|
+
];
|
|
11
15
|
function flag(args, name) {
|
|
12
16
|
const i = args.indexOf(name);
|
|
13
17
|
return i >= 0 ? args[i + 1] : undefined;
|
|
@@ -25,7 +29,9 @@ export async function runMachineDaemonCommand(subcommand, args) {
|
|
|
25
29
|
// The bundles this package ships beside its dist (dist/machine-daemon/cli.js -> dist/machine-daemon.js, dist/machine-toolkit.js).
|
|
26
30
|
const dist = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
27
31
|
await installDaemon(productionInstallDeps({
|
|
28
|
-
dashboardUrl: flag(args, "--dashboard-url") ??
|
|
32
|
+
dashboardUrl: flag(args, "--dashboard-url") ??
|
|
33
|
+
process.env.DANXBOT_DASHBOARD_URL ??
|
|
34
|
+
DEFAULT_DASHBOARD_URL,
|
|
29
35
|
kind: kindFlag,
|
|
30
36
|
profile: flag(args, "--profile") ?? null,
|
|
31
37
|
bundleSource: join(dist, "machine-daemon.js"),
|
|
@@ -35,7 +41,12 @@ export async function runMachineDaemonCommand(subcommand, args) {
|
|
|
35
41
|
}
|
|
36
42
|
if (subcommand === "uninstall-daemon") {
|
|
37
43
|
const autostart = productionAutostartDeps(join(daemonBundleDir(home), "machine-daemon.js"));
|
|
38
|
-
await uninstallDaemon({
|
|
44
|
+
await uninstallDaemon({
|
|
45
|
+
home,
|
|
46
|
+
fetchFn: fetch,
|
|
47
|
+
autostart,
|
|
48
|
+
print: (line) => console.log(line),
|
|
49
|
+
});
|
|
39
50
|
return 0;
|
|
40
51
|
}
|
|
41
52
|
throw new Error(`unknown machine daemon subcommand ${subcommand}`);
|
|
@@ -17,7 +17,7 @@ import { join } from "node:path";
|
|
|
17
17
|
import { claimAccessRequest } from "../access-request.js";
|
|
18
18
|
import { callMachineRegistration } from "../machine-registration.js";
|
|
19
19
|
import { machineSecretFor, machineSecretHome } from "../machine-secret.js";
|
|
20
|
-
import { installAutostart, productionAutostartDeps, removeAutostart } from "./autostart.js";
|
|
20
|
+
import { installAutostart, productionAutostartDeps, removeAutostart, } from "./autostart.js";
|
|
21
21
|
import { DAEMON_VERSION } from "./version.js";
|
|
22
22
|
import { toolkitBundlePath, daemonBundleDir, daemonConfigPath, daemonLogPath, daemonStatusPath, danxbotHome, machineIdentityPath, readJsonIfPresent, writeOwnerOnlyJson, } from "./state.js";
|
|
23
23
|
export const DEFAULT_DASHBOARD_URL = "https://app.50arms.ai";
|
|
@@ -27,7 +27,10 @@ function base(url) {
|
|
|
27
27
|
async function postJson(fetchFn, url, body, bearer) {
|
|
28
28
|
const res = await fetchFn(url, {
|
|
29
29
|
method: "POST",
|
|
30
|
-
headers: {
|
|
30
|
+
headers: {
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
...(bearer ? { Authorization: `Bearer ${bearer}` } : {}),
|
|
33
|
+
},
|
|
31
34
|
body: JSON.stringify(body),
|
|
32
35
|
});
|
|
33
36
|
const text = await res.text();
|
|
@@ -70,7 +73,13 @@ async function approveMachineKey(deps) {
|
|
|
70
73
|
}
|
|
71
74
|
/** Registers the machine with the approved key and returns the machine's own reporting bearer. */
|
|
72
75
|
async function registerWithKey(deps, machineId, machineKey) {
|
|
73
|
-
const res = await postJson(deps.fetchFn, `${base(deps.dashboardUrl)}/api/machines/register`, {
|
|
76
|
+
const res = await postJson(deps.fetchFn, `${base(deps.dashboardUrl)}/api/machines/register`, {
|
|
77
|
+
machine_id: machineId,
|
|
78
|
+
label: deps.label,
|
|
79
|
+
kind: deps.kind,
|
|
80
|
+
profile: deps.profile,
|
|
81
|
+
daemon_version: DAEMON_VERSION,
|
|
82
|
+
}, machineKey);
|
|
74
83
|
if (res.status !== 200)
|
|
75
84
|
throw new Error(`machine registration refused (HTTP ${res.status})`);
|
|
76
85
|
const bearer = res.json.bearer;
|
|
@@ -82,7 +91,9 @@ async function registerWithKey(deps, machineId, machineKey) {
|
|
|
82
91
|
* DX-7010 (DEC-670): a desktop's daemon joins its machine's one row at the same claim-code link every session uses. The dashboard mints the machine's id and the daemon's reporting token, and authentication after this is unchanged.
|
|
83
92
|
*/
|
|
84
93
|
async function registerDesktop(deps) {
|
|
85
|
-
const machineSecret = machineSecretFor(deps.dashboardUrl, {
|
|
94
|
+
const machineSecret = machineSecretFor(deps.dashboardUrl, {
|
|
95
|
+
home: deps.machineHome,
|
|
96
|
+
});
|
|
86
97
|
let shownCode = null;
|
|
87
98
|
for (;;) {
|
|
88
99
|
const call = await callMachineRegistration(deps.fetchFn, deps.dashboardUrl, {
|
|
@@ -119,7 +130,10 @@ async function registerWorkerHost(deps) {
|
|
|
119
130
|
const existing = readJsonIfPresent(machineIdentityPath(deps.home));
|
|
120
131
|
const machineId = existing?.machine_id ?? deps.newId();
|
|
121
132
|
const machineKey = await approveMachineKey(deps);
|
|
122
|
-
return {
|
|
133
|
+
return {
|
|
134
|
+
machineId,
|
|
135
|
+
bearer: await registerWithKey(deps, machineId, machineKey),
|
|
136
|
+
};
|
|
123
137
|
}
|
|
124
138
|
export async function installDaemon(deps) {
|
|
125
139
|
// DX-6537: checked before registering, which replaces the bearer of a daemon already running here: a half-installed package changes nothing.
|
|
@@ -127,8 +141,12 @@ export async function installDaemon(deps) {
|
|
|
127
141
|
if (!existsSync(source))
|
|
128
142
|
throw new Error(`this package has no ${source}: it cannot install the daemon`);
|
|
129
143
|
}
|
|
130
|
-
const { machineId, bearer } = deps.kind === "dev"
|
|
131
|
-
|
|
144
|
+
const { machineId, bearer } = deps.kind === "dev"
|
|
145
|
+
? await registerDesktop(deps)
|
|
146
|
+
: await registerWorkerHost(deps);
|
|
147
|
+
writeOwnerOnlyJson(machineIdentityPath(deps.home), {
|
|
148
|
+
machine_id: machineId,
|
|
149
|
+
});
|
|
132
150
|
const config = {
|
|
133
151
|
dashboard_url: base(deps.dashboardUrl),
|
|
134
152
|
machine_id: machineId,
|
|
@@ -169,7 +187,10 @@ export async function uninstallDaemon(deps) {
|
|
|
169
187
|
try {
|
|
170
188
|
const res = await deps.fetchFn(`${config.dashboard_url}/api/machines/retire`, {
|
|
171
189
|
method: "POST",
|
|
172
|
-
headers: {
|
|
190
|
+
headers: {
|
|
191
|
+
"Content-Type": "application/json",
|
|
192
|
+
Authorization: `Bearer ${config.bearer}`,
|
|
193
|
+
},
|
|
173
194
|
body: JSON.stringify({ machine_id: config.machine_id }),
|
|
174
195
|
});
|
|
175
196
|
if (res.status === 200)
|
|
@@ -181,7 +202,10 @@ export async function uninstallDaemon(deps) {
|
|
|
181
202
|
refusal = err instanceof Error ? err.message : String(err);
|
|
182
203
|
}
|
|
183
204
|
}
|
|
184
|
-
for (const file of [
|
|
205
|
+
for (const file of [
|
|
206
|
+
daemonConfigPath(deps.home),
|
|
207
|
+
daemonStatusPath(deps.home),
|
|
208
|
+
]) {
|
|
185
209
|
if (existsSync(file)) {
|
|
186
210
|
rmSync(file, { force: true });
|
|
187
211
|
}
|
|
@@ -24,9 +24,9 @@
|
|
|
24
24
|
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
25
25
|
import { homedir } from "node:os";
|
|
26
26
|
import { join } from "node:path";
|
|
27
|
-
import { LEFTOVER_COMMAND_MAX_CHARS } from "../leftover-wire.js";
|
|
27
|
+
import { LEFTOVER_COMMAND_MAX_CHARS, } from "../leftover-wire.js";
|
|
28
28
|
import { BEAT_INTERVAL_MS } from "./beat.js";
|
|
29
|
-
import { listProcesses, runCommand } from "../machine-toolkit/processes.js";
|
|
29
|
+
import { listProcesses, runCommand, } from "../machine-toolkit/processes.js";
|
|
30
30
|
export const FOLLOW_MIN_AGE_MS = 30 * 60 * 1000;
|
|
31
31
|
export const HOT_CPU_FRACTION = 0.5;
|
|
32
32
|
export const COOL_CPU_FRACTION = 0.2;
|
|
@@ -56,8 +56,13 @@ export function readClaudeSessionFiles(dir = join(homedir(), ".claude", "session
|
|
|
56
56
|
continue;
|
|
57
57
|
try {
|
|
58
58
|
const v = JSON.parse(readFileSync(join(dir, file), "utf8"));
|
|
59
|
-
if (typeof v.pid === "number" &&
|
|
60
|
-
|
|
59
|
+
if (typeof v.pid === "number" &&
|
|
60
|
+
typeof v.sessionId === "string" &&
|
|
61
|
+
typeof v.procStart === "string")
|
|
62
|
+
sessions.set(v.pid, {
|
|
63
|
+
sessionId: v.sessionId,
|
|
64
|
+
startedAtMs: fileTimeToMs(v.procStart),
|
|
65
|
+
});
|
|
61
66
|
}
|
|
62
67
|
catch {
|
|
63
68
|
continue;
|
|
@@ -66,17 +71,27 @@ export function readClaudeSessionFiles(dir = join(homedir(), ".claude", "session
|
|
|
66
71
|
return sessions;
|
|
67
72
|
}
|
|
68
73
|
const FOLLOW_ARG = /(?:^|\s)(?:-[a-zA-Z]*[fF][a-zA-Z]*|--follow(?:=\S+)?)(?=\s|$)/;
|
|
69
|
-
const FILTER_NAMES = new Set([
|
|
74
|
+
const FILTER_NAMES = new Set([
|
|
75
|
+
"grep.exe",
|
|
76
|
+
"egrep.exe",
|
|
77
|
+
"fgrep.exe",
|
|
78
|
+
"sed.exe",
|
|
79
|
+
"awk.exe",
|
|
80
|
+
"gawk.exe",
|
|
81
|
+
]);
|
|
70
82
|
/** Whether the row is a follower (`tail -f`) or the line-buffered reader of one's pipe. */
|
|
71
83
|
export function isFollowRow(row) {
|
|
72
84
|
const name = row.name.toLowerCase();
|
|
73
85
|
if (name === "tail.exe")
|
|
74
86
|
return FOLLOW_ARG.test(row.commandLine.replace(/^"[^"]*"|^\S+/, ""));
|
|
75
|
-
return FILTER_NAMES.has(name) &&
|
|
87
|
+
return (FILTER_NAMES.has(name) &&
|
|
88
|
+
/(?:^|\s)--line-buffered(?=\s|$)/.test(row.commandLine));
|
|
76
89
|
}
|
|
77
90
|
function shortCommand(commandLine) {
|
|
78
91
|
const flat = commandLine.replace(/\s+/g, " ").trim();
|
|
79
|
-
return flat.length > LEFTOVER_COMMAND_MAX_CHARS
|
|
92
|
+
return flat.length > LEFTOVER_COMMAND_MAX_CHARS
|
|
93
|
+
? `${flat.slice(0, LEFTOVER_COMMAND_MAX_CHARS)}...`
|
|
94
|
+
: flat;
|
|
80
95
|
}
|
|
81
96
|
/** The session id in each of `pids`' environments (null: the variable is absent). Windows only: the block is read through `NtQueryInformationProcess` and `ReadProcessMemory`, same user, no elevation. */
|
|
82
97
|
export const ENV_READ_SCRIPT = (pids) => `
|
|
@@ -145,7 +160,11 @@ export async function readMsysParents(processes, run = runCommand, exists = exis
|
|
|
145
160
|
for (const line of stdout.split(/\r?\n/)) {
|
|
146
161
|
const m = /^[\sI]*(\d+)\s+(\d+)\s+\d+\s+(\d+)\s/.exec(line);
|
|
147
162
|
if (m)
|
|
148
|
-
rows.push({
|
|
163
|
+
rows.push({
|
|
164
|
+
pid: Number(m[1]),
|
|
165
|
+
ppid: Number(m[2]),
|
|
166
|
+
winpid: Number(m[3]),
|
|
167
|
+
});
|
|
149
168
|
}
|
|
150
169
|
const winpidOfMsysPid = new Map(rows.filter((r) => r.pid !== r.winpid).map((r) => [r.pid, r.winpid]));
|
|
151
170
|
const parents = new Map();
|
|
@@ -168,7 +187,9 @@ export function createLeftoverTracker(deps) {
|
|
|
168
187
|
if (parent === undefined)
|
|
169
188
|
return undefined;
|
|
170
189
|
// A parent that started after its child is a later process that reused the id.
|
|
171
|
-
if (parent.startedAt !== null &&
|
|
190
|
+
if (parent.startedAt !== null &&
|
|
191
|
+
row.startedAt !== null &&
|
|
192
|
+
parent.startedAt.getTime() > row.startedAt.getTime())
|
|
172
193
|
return undefined;
|
|
173
194
|
return parent;
|
|
174
195
|
};
|
|
@@ -178,11 +199,20 @@ export function createLeftoverTracker(deps) {
|
|
|
178
199
|
for (let hop = 0; hop < MAX_CHAIN_HOPS; hop++) {
|
|
179
200
|
const parent = parentRowOf(child, byPid, msys);
|
|
180
201
|
if (parent === undefined)
|
|
181
|
-
return {
|
|
202
|
+
return {
|
|
203
|
+
owner: null,
|
|
204
|
+
walk: `ends at pid ${child.pid} ${child.name}: its parent pid ${msys.get(child.pid) ?? child.ppid} is not running`,
|
|
205
|
+
};
|
|
182
206
|
const session = sessions.get(parent.pid);
|
|
183
207
|
// The recorded start must match the running process: a session file left by an older `claude.exe` with the same pid names another session.
|
|
184
|
-
if (session !== undefined &&
|
|
185
|
-
|
|
208
|
+
if (session !== undefined &&
|
|
209
|
+
parent.name.toLowerCase() === "claude.exe" &&
|
|
210
|
+
parent.startedAt !== null &&
|
|
211
|
+
Math.abs(parent.startedAt.getTime() - session.startedAtMs) < 5000)
|
|
212
|
+
return {
|
|
213
|
+
owner: session.sessionId,
|
|
214
|
+
walk: `reaches claude.exe pid ${parent.pid} after ${hop + 1} hops`,
|
|
215
|
+
};
|
|
186
216
|
child = parent;
|
|
187
217
|
}
|
|
188
218
|
return { owner: null, walk: `no claude.exe within ${MAX_CHAIN_HOPS} hops` };
|
|
@@ -196,7 +226,10 @@ export function createLeftoverTracker(deps) {
|
|
|
196
226
|
const byPid = new Map(windows.map((r) => [r.pid, r]));
|
|
197
227
|
const sessions = deps.claudeSessions();
|
|
198
228
|
const msys = await deps.msysParents(windows);
|
|
199
|
-
liveSessionIds = new Set(windows
|
|
229
|
+
liveSessionIds = new Set(windows
|
|
230
|
+
.filter((r) => r.name.toLowerCase() === "claude.exe" &&
|
|
231
|
+
sessions.get(r.pid) !== undefined)
|
|
232
|
+
.map((r) => sessions.get(r.pid).sessionId));
|
|
200
233
|
const nowLive = new Map();
|
|
201
234
|
const nowMs = at.getTime();
|
|
202
235
|
for (const row of windows) {
|
|
@@ -210,7 +243,25 @@ export function createLeftoverTracker(deps) {
|
|
|
210
243
|
const shell = parentRowOf(row, byPid, msys);
|
|
211
244
|
if (follower)
|
|
212
245
|
deps.log(`leftover: first sight of follower pid ${row.pid} ${row.name} (${shortCommand(row.commandLine)}), started ${row.startedAt.toISOString()}: launching shell ${shell === undefined ? "not running" : `pid ${shell.pid} ${shell.name}`}; owner ${owner ?? "none"}, chain ${walk}`);
|
|
213
|
-
entry = {
|
|
246
|
+
entry = {
|
|
247
|
+
pid: row.pid,
|
|
248
|
+
startedAtMs,
|
|
249
|
+
owner,
|
|
250
|
+
envChecked: false,
|
|
251
|
+
shellKey: shell?.startedAt
|
|
252
|
+
? keyOf(shell.pid, shell.startedAt.getTime())
|
|
253
|
+
: null,
|
|
254
|
+
unreachable: false,
|
|
255
|
+
awaiting: false,
|
|
256
|
+
disposition: null,
|
|
257
|
+
lastCpu100ns: null,
|
|
258
|
+
lastSeenMs: nowMs,
|
|
259
|
+
hotSinceMs: null,
|
|
260
|
+
hotCpuStart100ns: null,
|
|
261
|
+
hotStartWallMs: null,
|
|
262
|
+
lastCpuPercent: null,
|
|
263
|
+
remindedAtMs: null,
|
|
264
|
+
};
|
|
214
265
|
ledger.set(key, entry);
|
|
215
266
|
}
|
|
216
267
|
else if (entry.owner === null && owner !== null) {
|
|
@@ -218,8 +269,12 @@ export function createLeftoverTracker(deps) {
|
|
|
218
269
|
if (follower)
|
|
219
270
|
deps.log(`leftover: follower pid ${row.pid} now has owner ${owner} (chain ${walk})`);
|
|
220
271
|
}
|
|
221
|
-
if (row.cpuTime100ns !== null &&
|
|
222
|
-
|
|
272
|
+
if (row.cpuTime100ns !== null &&
|
|
273
|
+
entry.lastCpu100ns !== null &&
|
|
274
|
+
nowMs > entry.lastSeenMs) {
|
|
275
|
+
const fraction = (row.cpuTime100ns - entry.lastCpu100ns) /
|
|
276
|
+
10000 /
|
|
277
|
+
(nowMs - entry.lastSeenMs);
|
|
223
278
|
if (fraction >= HOT_CPU_FRACTION && entry.hotSinceMs === null) {
|
|
224
279
|
entry.hotSinceMs = entry.lastSeenMs;
|
|
225
280
|
entry.hotCpuStart100ns = entry.lastCpu100ns;
|
|
@@ -229,8 +284,16 @@ export function createLeftoverTracker(deps) {
|
|
|
229
284
|
entry.hotSinceMs = null;
|
|
230
285
|
}
|
|
231
286
|
}
|
|
232
|
-
if (entry.hotSinceMs !== null &&
|
|
233
|
-
entry.
|
|
287
|
+
if (entry.hotSinceMs !== null &&
|
|
288
|
+
entry.hotCpuStart100ns !== null &&
|
|
289
|
+
entry.hotStartWallMs !== null &&
|
|
290
|
+
row.cpuTime100ns !== null &&
|
|
291
|
+
nowMs > entry.hotStartWallMs) {
|
|
292
|
+
entry.lastCpuPercent =
|
|
293
|
+
((row.cpuTime100ns - entry.hotCpuStart100ns) /
|
|
294
|
+
10000 /
|
|
295
|
+
(nowMs - entry.hotStartWallMs)) *
|
|
296
|
+
100;
|
|
234
297
|
}
|
|
235
298
|
entry.lastCpu100ns = row.cpuTime100ns;
|
|
236
299
|
entry.lastSeenMs = nowMs;
|
|
@@ -250,7 +313,10 @@ export function createLeftoverTracker(deps) {
|
|
|
250
313
|
lastSessionIds = sessionIds;
|
|
251
314
|
}
|
|
252
315
|
// A hot process nobody owns may still carry its session in its environment (python, node); read each once.
|
|
253
|
-
const unknown = [...ledger.entries()].filter(([, e]) => e.owner === null &&
|
|
316
|
+
const unknown = [...ledger.entries()].filter(([, e]) => e.owner === null &&
|
|
317
|
+
!e.envChecked &&
|
|
318
|
+
e.hotSinceMs !== null &&
|
|
319
|
+
nowMs - e.hotSinceMs >= HOT_MIN_MS);
|
|
254
320
|
if (unknown.length > 0 && nowMs >= envRetryAfterMs) {
|
|
255
321
|
let fromEnv;
|
|
256
322
|
try {
|
|
@@ -273,7 +339,9 @@ export function createLeftoverTracker(deps) {
|
|
|
273
339
|
const sessions = new Map();
|
|
274
340
|
const reminded = new Map();
|
|
275
341
|
const ownerless = [];
|
|
276
|
-
const awaiting = [
|
|
342
|
+
const awaiting = [
|
|
343
|
+
...new Set([...ledger.values()].flatMap((e) => e.awaiting && e.owner !== null ? [e.owner] : [])),
|
|
344
|
+
].sort();
|
|
277
345
|
// DX-7107: after a failed sample the ledger still holds processes that may have ended since; a reminder names only what the table showed lately.
|
|
278
346
|
if (sampledAtMs === null || nowMs - sampledAtMs > SAMPLE_MAX_AGE_MS) {
|
|
279
347
|
if (sampledAtMs !== null)
|
|
@@ -286,9 +354,14 @@ export function createLeftoverTracker(deps) {
|
|
|
286
354
|
continue;
|
|
287
355
|
const ageMs = nowMs - entry.startedAtMs;
|
|
288
356
|
const follower = isFollowRow(row);
|
|
289
|
-
const readerGone = follower &&
|
|
357
|
+
const readerGone = follower &&
|
|
358
|
+
entry.shellKey !== null &&
|
|
359
|
+
!live.has(entry.shellKey) &&
|
|
360
|
+
ageMs >= READER_GONE_MIN_AGE_MS;
|
|
290
361
|
const follow = follower && ageMs >= FOLLOW_MIN_AGE_MS;
|
|
291
|
-
const hot = entry.owner !== null &&
|
|
362
|
+
const hot = entry.owner !== null &&
|
|
363
|
+
entry.hotSinceMs !== null &&
|
|
364
|
+
nowMs - entry.hotSinceMs >= HOT_MIN_MS;
|
|
292
365
|
if (!readerGone && !follow && !hot) {
|
|
293
366
|
entry.disposition = null;
|
|
294
367
|
continue;
|
|
@@ -297,7 +370,9 @@ export function createLeftoverTracker(deps) {
|
|
|
297
370
|
pid: entry.pid,
|
|
298
371
|
command: shortCommand(row.commandLine),
|
|
299
372
|
age_seconds: Math.floor(ageMs / 1000),
|
|
300
|
-
cpu_percent: hot && entry.lastCpuPercent !== null
|
|
373
|
+
cpu_percent: hot && entry.lastCpuPercent !== null
|
|
374
|
+
? Math.round(entry.lastCpuPercent)
|
|
375
|
+
: null,
|
|
301
376
|
why: readerGone ? "reader_gone" : follow ? "follow" : "cpu",
|
|
302
377
|
};
|
|
303
378
|
// A process is logged when it is first flagged, when what becomes of it changes, and each time a reminder goes out; a standing answer (and "reminded recently", which only waits) is not repeated every report.
|
|
@@ -316,10 +391,20 @@ export function createLeftoverTracker(deps) {
|
|
|
316
391
|
}
|
|
317
392
|
else {
|
|
318
393
|
if (entry.unreachable)
|
|
319
|
-
ownerless.push({
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
394
|
+
ownerless.push({
|
|
395
|
+
...process,
|
|
396
|
+
reason: "session not on this dashboard",
|
|
397
|
+
});
|
|
398
|
+
if (entry.remindedAtMs === null ||
|
|
399
|
+
nowMs - entry.remindedAtMs >= REMIND_INTERVAL_MS) {
|
|
400
|
+
sessions.set(entry.owner, [
|
|
401
|
+
...(sessions.get(entry.owner) ?? []),
|
|
402
|
+
process,
|
|
403
|
+
]);
|
|
404
|
+
reminded.set(entry.owner, [
|
|
405
|
+
...(reminded.get(entry.owner) ?? []),
|
|
406
|
+
key,
|
|
407
|
+
]);
|
|
323
408
|
note("reminded", "the session is reminded in this report", true);
|
|
324
409
|
}
|
|
325
410
|
else {
|
|
@@ -327,7 +412,15 @@ export function createLeftoverTracker(deps) {
|
|
|
327
412
|
}
|
|
328
413
|
}
|
|
329
414
|
}
|
|
330
|
-
return {
|
|
415
|
+
return {
|
|
416
|
+
sessions: [...sessions].map(([session_id, processes]) => ({
|
|
417
|
+
session_id,
|
|
418
|
+
processes,
|
|
419
|
+
})),
|
|
420
|
+
ownerless,
|
|
421
|
+
reminded,
|
|
422
|
+
awaiting,
|
|
423
|
+
};
|
|
331
424
|
},
|
|
332
425
|
settle(collection, answer, at) {
|
|
333
426
|
for (const sessionId of collection.awaiting) {
|
|
@@ -341,7 +434,9 @@ export function createLeftoverTracker(deps) {
|
|
|
341
434
|
if (expired)
|
|
342
435
|
entry.remindedAtMs = null;
|
|
343
436
|
}
|
|
344
|
-
deps.log(expired
|
|
437
|
+
deps.log(expired
|
|
438
|
+
? `leftover: the reminder of session ${sessionId} expired on the dashboard before its stream took it, so it is sent again at the next report`
|
|
439
|
+
: `leftover: the stream of session ${sessionId} took its reminder`);
|
|
345
440
|
}
|
|
346
441
|
for (const [sessionId, keys] of collection.reminded) {
|
|
347
442
|
const unreachable = answer.undelivered.includes(sessionId);
|
|
@@ -363,7 +458,16 @@ export function createLeftoverTracker(deps) {
|
|
|
363
458
|
/** The real tracker: the Windows process table (no WSL listing: its rows could not be mapped to a session), Git's `ps -W`, `~/.claude/sessions` and the environment read. The watchdog is Windows-only: on another platform nothing is ever watched. */
|
|
364
459
|
export function createSystemLeftoverTracker(log, run = runCommand, platform = process.platform) {
|
|
365
460
|
if (platform !== "win32")
|
|
366
|
-
return {
|
|
461
|
+
return {
|
|
462
|
+
sample: async () => { },
|
|
463
|
+
collect: () => ({
|
|
464
|
+
sessions: [],
|
|
465
|
+
ownerless: [],
|
|
466
|
+
reminded: new Map(),
|
|
467
|
+
awaiting: [],
|
|
468
|
+
}),
|
|
469
|
+
settle: () => { },
|
|
470
|
+
};
|
|
367
471
|
return createLeftoverTracker({
|
|
368
472
|
log,
|
|
369
473
|
listing: (now) => listProcesses(now, run, platform, undefined, "never"),
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* due, and the last health report's time, outcome and error.
|
|
10
10
|
* machine-daemon/ — the copies of the daemon and toolkit bundles the autostart entry runs (so a package update never changes a running file).
|
|
11
11
|
*/
|
|
12
|
-
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, } from "node:fs";
|
|
13
13
|
import { homedir } from "node:os";
|
|
14
14
|
import { dirname, join } from "node:path";
|
|
15
15
|
export const MACHINE_DAEMON_DIR_NAME = ".danxbot";
|
|
@@ -56,7 +56,10 @@ export function toolkitBundlePath(home = danxbotHome()) {
|
|
|
56
56
|
export function writeOwnerOnlyJson(file, value) {
|
|
57
57
|
mkdirSync(dirname(file), { recursive: true });
|
|
58
58
|
const tmp = `${file}.${process.pid}.tmp`;
|
|
59
|
-
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, {
|
|
59
|
+
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}\n`, {
|
|
60
|
+
encoding: "utf8",
|
|
61
|
+
mode: 0o600,
|
|
62
|
+
});
|
|
60
63
|
if (process.platform !== "win32")
|
|
61
64
|
chmodSync(tmp, 0o600);
|
|
62
65
|
renameSync(tmp, file);
|
|
@@ -81,7 +84,10 @@ export function createDaemonLog(file, now = () => new Date(), maxBytes = DAEMON_
|
|
|
81
84
|
narrowed = true;
|
|
82
85
|
if (existsSync(file) && statSync(file).size >= maxBytes)
|
|
83
86
|
renameSync(file, `${file}.1`);
|
|
84
|
-
appendFileSync(file, `${now().toISOString()} ${line}\n`, {
|
|
87
|
+
appendFileSync(file, `${now().toISOString()} ${line}\n`, {
|
|
88
|
+
encoding: "utf8",
|
|
89
|
+
mode: 0o600,
|
|
90
|
+
});
|
|
85
91
|
}
|
|
86
92
|
catch (err) {
|
|
87
93
|
console.error(`[machine-daemon] cannot write ${file}: ${err instanceof Error ? err.message : String(err)}`);
|