@50arms/platform-mcp 0.1.457 → 0.1.459
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/index.js +3 -3
- package/dist/listen.js +3 -1
- package/dist/machine-daemon/install.js +2 -1
- package/dist/machine-daemon/leftovers.js +38 -11
- package/dist/machine-daemon/state.js +20 -1
- package/dist/machine-daemon.js +57 -12
- package/dist/machine-toolkit.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -421,13 +421,13 @@ yours too) is a call to it. Find a route with \`50arms_api_list\`. Read its exac
|
|
|
421
421
|
|
|
422
422
|
RESOURCES. Most collections take GET (list) and POST (create); a single resource (\`<collection>/<id>\`) takes
|
|
423
423
|
GET, PATCH or PUT (send only the fields you change) and DELETE. A card is edited at
|
|
424
|
-
\`PATCH /api/issues/<id>/edit\`. A card get
|
|
424
|
+
\`PATCH /api/issues/<id>/edit\`. A card get or list answers the compact row, a list one page of 50 with
|
|
425
425
|
\`total\` (page with \`limit\` and \`offset\`). Name each other field or relation you need in the \`fields\` query
|
|
426
426
|
tree. \`GET /api/resources/<resource>/fields\` lists the names.
|
|
427
427
|
|
|
428
428
|
STALE HASH. A write to contested text sends back the \`content_hash\` or \`base_hash\` you read. A stale content write is
|
|
429
429
|
never refused: \`collided\` says what it met, so re-read. Only 409 \`merge_base_unknown\` is: reapply your
|
|
430
|
-
change to the text and hash it returns. Settings and sign-offs still refuse a stale hash: re-read
|
|
430
|
+
change to the text and hash it returns. Settings and sign-offs still refuse a stale hash: re-read and retry.
|
|
431
431
|
|
|
432
432
|
REFUSALS. A 4xx body is \`{error, ...}\`, and its other fields name the fix: \`offending_keys\`, \`failed_gate\`,
|
|
433
433
|
\`unresolved_items\`, \`did_you_mean\`.
|
|
@@ -435,7 +435,7 @@ REFUSALS. A 4xx body is \`{error, ...}\`, and its other fields name the fix: \`o
|
|
|
435
435
|
WRITING STANDARD, for every title, summary, description, comment, problem, solution and plan record you write. The
|
|
436
436
|
reader is new to the subject and will not read the code or docs: what you write is their documentation. Open with
|
|
437
437
|
plain background (what it is, why, what is wrong or changes) before evidence.
|
|
438
|
-
Define each term a newcomer would not know. Cite exact ids (\`PBLM-2757\`, not "problem 2757"), SHAs and paths. Be brief; never transcribe reasoning. A title or summary alone must let the reader decide.
|
|
438
|
+
Define each term a newcomer would not know. Cite exact ids (\`PBLM-2757\`, not "problem 2757"), SHAs and paths. Card ids render with their plan; never add it. Be brief; never transcribe reasoning. A title or summary alone must let the reader decide.
|
|
439
439
|
|
|
440
440
|
DESIGN. UI designs and style options go on the card they concern (\`mini_app_publish\`), never an Artifact or HTML page.
|
|
441
441
|
Options are design states, never a chat or text question; a person chooses.
|
package/dist/listen.js
CHANGED
|
@@ -311,7 +311,9 @@ function describe(event) {
|
|
|
311
311
|
const reply = !isPersonsComment(event) ? "" : `. ${onPlan ? planReplyInstruction(event.planId, d.comment_id) : replyInstruction(event.cardId, d.comment_id)}`;
|
|
312
312
|
if (problem === undefined)
|
|
313
313
|
return `${who} commented${onPlan ? " on the plan" : ""}: ${quoted(d.excerpt)}${reply}`;
|
|
314
|
-
|
|
314
|
+
// DX-7108 (operator correction, comment 21778): a person's comment that answers the problem is recorded as its answer by the session, quoting the comment.
|
|
315
|
+
const answerHint = !isPersonsComment(event) || onPlan ? "" : `. If the comment decides the problem, record it as the answer in the same turn: POST /api/issues/${event.cardId}/problems/${problem.id}/answer with the chosen solution_id or freeform and provenance {"source":"comment","source_ref":"${d.comment_id}","quote":"<their words>"}`;
|
|
316
|
+
return `${who} commented on problem ${quoted(problem.statement)}: ${quoted(d.excerpt)}${reply}${answerHint}`;
|
|
315
317
|
}
|
|
316
318
|
case "solution_answered": {
|
|
317
319
|
// DX-2735: every answer answers ONE problem on a card that may carry several,
|
|
@@ -19,7 +19,7 @@ import { callMachineRegistration } from "../machine-registration.js";
|
|
|
19
19
|
import { machineSecretFor, machineSecretHome } from "../machine-secret.js";
|
|
20
20
|
import { installAutostart, productionAutostartDeps, removeAutostart } from "./autostart.js";
|
|
21
21
|
import { DAEMON_VERSION } from "./version.js";
|
|
22
|
-
import { toolkitBundlePath, daemonBundleDir, daemonConfigPath, daemonStatusPath, danxbotHome, machineIdentityPath, readJsonIfPresent, writeOwnerOnlyJson, } from "./state.js";
|
|
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";
|
|
24
24
|
function base(url) {
|
|
25
25
|
return url.replace(/\/+$/, "");
|
|
@@ -156,6 +156,7 @@ export function daemonStatusText(home) {
|
|
|
156
156
|
lines.push(`Last beat: ${status?.last_beat_at ?? "none yet"}${status ? (status.last_beat_ok ? " (ok)" : ` (failed: ${status.last_error ?? "unknown"})`) : ""}`);
|
|
157
157
|
lines.push(`Next beat: ${status?.next_beat_at ?? "none yet"}`);
|
|
158
158
|
lines.push(`Last report: ${status?.last_report_at ?? "none yet"}${status?.last_report_outcome ? ` (${status.last_report_outcome}${status.last_report_error ? `: ${status.last_report_error}` : ""})` : ""}`);
|
|
159
|
+
lines.push(`Log: ${daemonLogPath(home)}`);
|
|
159
160
|
return lines.join("\n");
|
|
160
161
|
}
|
|
161
162
|
export async function uninstallDaemon(deps) {
|
|
@@ -96,9 +96,10 @@ public static class PEnv {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
'@
|
|
99
|
+
$name = 'CLAUDE_CODE_SESSION_ID='
|
|
99
100
|
foreach ($id in @(${pids.join(",")})) {
|
|
100
|
-
$hit = ([PEnv]::Env([int]$id)).Split([char]0) | Where-Object { $_
|
|
101
|
-
if ($hit) { "$id=" + $hit.Substring(
|
|
101
|
+
$hit = ([PEnv]::Env([int]$id)).Split([char]0) | Where-Object { $_.StartsWith($name) } | Select-Object -First 1
|
|
102
|
+
if ($hit) { "$id=" + $hit.Substring($name.Length) }
|
|
102
103
|
}
|
|
103
104
|
`;
|
|
104
105
|
const SESSION_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
@@ -155,6 +156,7 @@ export function createLeftoverTracker(deps) {
|
|
|
155
156
|
let live = new Map();
|
|
156
157
|
let liveSessionIds = new Set();
|
|
157
158
|
let envRetryAfterMs = 0;
|
|
159
|
+
let lastSessionIds = "";
|
|
158
160
|
const parentRowOf = (row, byPid, msys) => {
|
|
159
161
|
const parent = byPid.get(msys.get(row.pid) ?? row.ppid);
|
|
160
162
|
if (parent === undefined)
|
|
@@ -164,19 +166,20 @@ export function createLeftoverTracker(deps) {
|
|
|
164
166
|
return undefined;
|
|
165
167
|
return parent;
|
|
166
168
|
};
|
|
167
|
-
|
|
169
|
+
/** The session whose `claude.exe` is an ancestor of `row`, and where the walk ended, for the log: a process whose chain breaks at a shell that already exited is the case the ledger exists for. */
|
|
170
|
+
const traceOwner = (row, byPid, msys, sessions) => {
|
|
168
171
|
let child = row;
|
|
169
172
|
for (let hop = 0; hop < MAX_CHAIN_HOPS; hop++) {
|
|
170
173
|
const parent = parentRowOf(child, byPid, msys);
|
|
171
174
|
if (parent === undefined)
|
|
172
|
-
return null;
|
|
175
|
+
return { owner: null, walk: `ends at pid ${child.pid} ${child.name}: its parent pid ${msys.get(child.pid) ?? child.ppid} is not running` };
|
|
173
176
|
const session = sessions.get(parent.pid);
|
|
174
177
|
// The recorded start must match the running process: a session file left by an older `claude.exe` with the same pid names another session.
|
|
175
178
|
if (session !== undefined && parent.name.toLowerCase() === "claude.exe" && parent.startedAt !== null && Math.abs(parent.startedAt.getTime() - session.startedAtMs) < 5000)
|
|
176
|
-
return session.sessionId;
|
|
179
|
+
return { owner: session.sessionId, walk: `reaches claude.exe pid ${parent.pid} after ${hop + 1} hops` };
|
|
177
180
|
child = parent;
|
|
178
181
|
}
|
|
179
|
-
return null;
|
|
182
|
+
return { owner: null, walk: `no claude.exe within ${MAX_CHAIN_HOPS} hops` };
|
|
180
183
|
};
|
|
181
184
|
return {
|
|
182
185
|
async sample(at) {
|
|
@@ -195,14 +198,19 @@ export function createLeftoverTracker(deps) {
|
|
|
195
198
|
const key = keyOf(row.pid, startedAtMs);
|
|
196
199
|
nowLive.set(key, row);
|
|
197
200
|
let entry = ledger.get(key);
|
|
198
|
-
const owner =
|
|
201
|
+
const { owner, walk } = traceOwner(row, byPid, msys, sessions);
|
|
202
|
+
const follower = isFollowRow(row);
|
|
199
203
|
if (entry === undefined) {
|
|
200
204
|
const shell = parentRowOf(row, byPid, msys);
|
|
205
|
+
if (follower)
|
|
206
|
+
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}`);
|
|
201
207
|
entry = { pid: row.pid, startedAtMs, owner, envChecked: false, shellKey: shell?.startedAt ? keyOf(shell.pid, shell.startedAt.getTime()) : null, unreachable: false, lastCpu100ns: null, lastSeenMs: nowMs, hotSinceMs: null, hotCpuStart100ns: null, hotStartWallMs: null, lastCpuPercent: null, remindedAtMs: null };
|
|
202
208
|
ledger.set(key, entry);
|
|
203
209
|
}
|
|
204
210
|
else if (entry.owner === null && owner !== null) {
|
|
205
211
|
entry.owner = owner;
|
|
212
|
+
if (follower)
|
|
213
|
+
deps.log(`leftover: follower pid ${row.pid} now has owner ${owner} (chain ${walk})`);
|
|
206
214
|
}
|
|
207
215
|
if (row.cpuTime100ns !== null && entry.lastCpu100ns !== null && nowMs > entry.lastSeenMs) {
|
|
208
216
|
const fraction = (row.cpuTime100ns - entry.lastCpu100ns) / 10000 / (nowMs - entry.lastSeenMs);
|
|
@@ -221,10 +229,19 @@ export function createLeftoverTracker(deps) {
|
|
|
221
229
|
entry.lastCpu100ns = row.cpuTime100ns;
|
|
222
230
|
entry.lastSeenMs = nowMs;
|
|
223
231
|
}
|
|
224
|
-
for (const key of [...ledger
|
|
225
|
-
if (
|
|
226
|
-
|
|
232
|
+
for (const [key, entry] of [...ledger]) {
|
|
233
|
+
if (nowLive.has(key))
|
|
234
|
+
continue;
|
|
235
|
+
if (live.has(key) && isFollowRow(live.get(key)))
|
|
236
|
+
deps.log(`leftover: follower pid ${entry.pid} ended (owner ${entry.owner ?? "none"})`);
|
|
237
|
+
ledger.delete(key);
|
|
238
|
+
}
|
|
227
239
|
live = nowLive;
|
|
240
|
+
const sessionIds = [...liveSessionIds].sort().join(",");
|
|
241
|
+
if (sessionIds !== lastSessionIds) {
|
|
242
|
+
deps.log(`leftover: watching ${liveSessionIds.size} claude sessions: ${sessionIds}`);
|
|
243
|
+
lastSessionIds = sessionIds;
|
|
244
|
+
}
|
|
228
245
|
// A hot process nobody owns may still carry its session in its environment (python, node); read each once.
|
|
229
246
|
const unknown = [...ledger.entries()].filter(([, e]) => e.owner === null && !e.envChecked && e.hotSinceMs !== null && nowMs - e.hotSinceMs >= HOT_MIN_MS);
|
|
230
247
|
if (unknown.length > 0 && nowMs >= envRetryAfterMs) {
|
|
@@ -240,6 +257,7 @@ export function createLeftoverTracker(deps) {
|
|
|
240
257
|
for (const [, e] of unknown) {
|
|
241
258
|
e.envChecked = true;
|
|
242
259
|
e.owner = fromEnv.get(e.pid) ?? null;
|
|
260
|
+
deps.log(`leftover: hot process pid ${e.pid} has no claude.exe ancestor; its environment names ${e.owner ?? "no session"}`);
|
|
243
261
|
}
|
|
244
262
|
}
|
|
245
263
|
},
|
|
@@ -266,11 +284,14 @@ export function createLeftoverTracker(deps) {
|
|
|
266
284
|
cpu_percent: hot && entry.lastCpuPercent !== null ? Math.round(entry.lastCpuPercent) : null,
|
|
267
285
|
why: readerGone ? "reader_gone" : follow ? "follow" : "cpu",
|
|
268
286
|
};
|
|
287
|
+
const flagged = `leftover: flagged pid ${entry.pid} ${row.name} (${process.why}, ${process.age_seconds} s old, owner ${entry.owner ?? "none"}):`;
|
|
269
288
|
if (entry.owner === null) {
|
|
270
289
|
ownerless.push({ ...process, reason: "no session found" });
|
|
290
|
+
deps.log(`${flagged} no session found, so it goes to the orphan list`);
|
|
271
291
|
}
|
|
272
292
|
else if (!liveSessionIds.has(entry.owner)) {
|
|
273
293
|
ownerless.push({ ...process, reason: "session ended" });
|
|
294
|
+
deps.log(`${flagged} its session has ended, so it goes to the orphan list`);
|
|
274
295
|
}
|
|
275
296
|
else {
|
|
276
297
|
if (entry.unreachable)
|
|
@@ -278,6 +299,10 @@ export function createLeftoverTracker(deps) {
|
|
|
278
299
|
if (entry.remindedAtMs === null || nowMs - entry.remindedAtMs >= REMIND_INTERVAL_MS) {
|
|
279
300
|
sessions.set(entry.owner, [...(sessions.get(entry.owner) ?? []), process]);
|
|
280
301
|
reminded.set(entry.owner, [...(reminded.get(entry.owner) ?? []), key]);
|
|
302
|
+
deps.log(`${flagged} the session is reminded in this report`);
|
|
303
|
+
}
|
|
304
|
+
else {
|
|
305
|
+
deps.log(`${flagged} the session was reminded ${Math.floor((nowMs - entry.remindedAtMs) / 60_000)} min ago, so not again yet`);
|
|
281
306
|
}
|
|
282
307
|
}
|
|
283
308
|
}
|
|
@@ -294,15 +319,17 @@ export function createLeftoverTracker(deps) {
|
|
|
294
319
|
if (!entry.unreachable)
|
|
295
320
|
entry.remindedAtMs = at.getTime();
|
|
296
321
|
}
|
|
322
|
+
deps.log(`leftover: the dashboard ${undelivered.includes(sessionId) ? "has no stream row for" : "took the reminder for"} session ${sessionId} (${keys.length} processes)`);
|
|
297
323
|
}
|
|
298
324
|
},
|
|
299
325
|
};
|
|
300
326
|
}
|
|
301
327
|
/** 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. */
|
|
302
|
-
export function createSystemLeftoverTracker(run = runCommand, platform = process.platform) {
|
|
328
|
+
export function createSystemLeftoverTracker(log, run = runCommand, platform = process.platform) {
|
|
303
329
|
if (platform !== "win32")
|
|
304
330
|
return { sample: async () => { }, collect: () => ({ sessions: [], ownerless: [], reminded: new Map() }), settle: () => { } };
|
|
305
331
|
return createLeftoverTracker({
|
|
332
|
+
log,
|
|
306
333
|
listing: (now) => listProcesses(now, run, platform, undefined, "never"),
|
|
307
334
|
msysParents: (rows) => readMsysParents(rows, run),
|
|
308
335
|
claudeSessions: () => readClaudeSessionFiles(),
|
|
@@ -4,11 +4,12 @@
|
|
|
4
4
|
* machine.json — `{ machine_id }`: the stable id the daemon made at install (never the hostname).
|
|
5
5
|
* machine-daemon.json — the dashboard URL, the machine id and its reporting bearer, owner-only (0600). The bearer is
|
|
6
6
|
* the daemon's only credential; it is never printed or logged.
|
|
7
|
+
* machine-daemon.log — what the daemon logs, one timestamped line each (DX-7107: the autostart entry runs headless, so stdout is lost); at most {@link DAEMON_LOG_MAX_BYTES}, then it moves to machine-daemon.log.1.
|
|
7
8
|
* machine-daemon-status.json — written after every beat and report: when it last beat, whether that beat landed, when the next is
|
|
8
9
|
* due, and the last health report's time, outcome and error.
|
|
9
10
|
* machine-daemon/ — the copies of the daemon and toolkit bundles the autostart entry runs (so a package update never changes a running file).
|
|
10
11
|
*/
|
|
11
|
-
import { chmodSync, existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from "node:fs";
|
|
12
13
|
import { homedir } from "node:os";
|
|
13
14
|
import { dirname, join } from "node:path";
|
|
14
15
|
export const MACHINE_DAEMON_DIR_NAME = ".danxbot";
|
|
@@ -42,6 +43,9 @@ export function daemonConfigPath(home = danxbotHome()) {
|
|
|
42
43
|
export function daemonStatusPath(home = danxbotHome()) {
|
|
43
44
|
return join(home, "machine-daemon-status.json");
|
|
44
45
|
}
|
|
46
|
+
export function daemonLogPath(home = danxbotHome()) {
|
|
47
|
+
return join(home, "machine-daemon.log");
|
|
48
|
+
}
|
|
45
49
|
export function daemonBundleDir(home = danxbotHome()) {
|
|
46
50
|
return join(home, "machine-daemon");
|
|
47
51
|
}
|
|
@@ -62,3 +66,18 @@ export function readJsonIfPresent(file) {
|
|
|
62
66
|
return null;
|
|
63
67
|
return JSON.parse(readFileSync(file, "utf8"));
|
|
64
68
|
}
|
|
69
|
+
export const DAEMON_LOG_MAX_BYTES = 1024 * 1024;
|
|
70
|
+
/** Appends `<iso time> <line>` to `file`. Past {@link DAEMON_LOG_MAX_BYTES} the file moves to `<file>.1` (replacing the older one) and a new one starts. A log that cannot be written is reported on stderr and never stops the daemon. */
|
|
71
|
+
export function createDaemonLog(file, now = () => new Date(), maxBytes = DAEMON_LOG_MAX_BYTES) {
|
|
72
|
+
return (line) => {
|
|
73
|
+
try {
|
|
74
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
75
|
+
if (existsSync(file) && statSync(file).size >= maxBytes)
|
|
76
|
+
renameSync(file, `${file}.1`);
|
|
77
|
+
appendFileSync(file, `${now().toISOString()} ${line}\n`, "utf8");
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
console.error(`[machine-daemon] cannot write ${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
package/dist/machine-daemon.js
CHANGED
|
@@ -357,9 +357,10 @@ public static class PEnv {
|
|
|
357
357
|
}
|
|
358
358
|
}
|
|
359
359
|
'@
|
|
360
|
+
$name = 'CLAUDE_CODE_SESSION_ID='
|
|
360
361
|
foreach ($id in @(${pids.join(",")})) {
|
|
361
|
-
$hit = ([PEnv]::Env([int]$id)).Split([char]0) | Where-Object { $_
|
|
362
|
-
if ($hit) { "$id=" + $hit.Substring(
|
|
362
|
+
$hit = ([PEnv]::Env([int]$id)).Split([char]0) | Where-Object { $_.StartsWith($name) } | Select-Object -First 1
|
|
363
|
+
if ($hit) { "$id=" + $hit.Substring($name.Length) }
|
|
363
364
|
}
|
|
364
365
|
`;
|
|
365
366
|
var SESSION_ID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
@@ -402,22 +403,23 @@ function createLeftoverTracker(deps) {
|
|
|
402
403
|
let live = /* @__PURE__ */ new Map();
|
|
403
404
|
let liveSessionIds = /* @__PURE__ */ new Set();
|
|
404
405
|
let envRetryAfterMs = 0;
|
|
406
|
+
let lastSessionIds = "";
|
|
405
407
|
const parentRowOf = (row, byPid, msys) => {
|
|
406
408
|
const parent = byPid.get(msys.get(row.pid) ?? row.ppid);
|
|
407
409
|
if (parent === void 0) return void 0;
|
|
408
410
|
if (parent.startedAt !== null && row.startedAt !== null && parent.startedAt.getTime() > row.startedAt.getTime()) return void 0;
|
|
409
411
|
return parent;
|
|
410
412
|
};
|
|
411
|
-
const
|
|
413
|
+
const traceOwner = (row, byPid, msys, sessions) => {
|
|
412
414
|
let child = row;
|
|
413
415
|
for (let hop = 0; hop < MAX_CHAIN_HOPS; hop++) {
|
|
414
416
|
const parent = parentRowOf(child, byPid, msys);
|
|
415
|
-
if (parent === void 0) return null;
|
|
417
|
+
if (parent === void 0) return { owner: null, walk: `ends at pid ${child.pid} ${child.name}: its parent pid ${msys.get(child.pid) ?? child.ppid} is not running` };
|
|
416
418
|
const session = sessions.get(parent.pid);
|
|
417
|
-
if (session !== void 0 && parent.name.toLowerCase() === "claude.exe" && parent.startedAt !== null && Math.abs(parent.startedAt.getTime() - session.startedAtMs) < 5e3) return session.sessionId;
|
|
419
|
+
if (session !== void 0 && parent.name.toLowerCase() === "claude.exe" && parent.startedAt !== null && Math.abs(parent.startedAt.getTime() - session.startedAtMs) < 5e3) return { owner: session.sessionId, walk: `reaches claude.exe pid ${parent.pid} after ${hop + 1} hops` };
|
|
418
420
|
child = parent;
|
|
419
421
|
}
|
|
420
|
-
return null;
|
|
422
|
+
return { owner: null, walk: `no claude.exe within ${MAX_CHAIN_HOPS} hops` };
|
|
421
423
|
};
|
|
422
424
|
return {
|
|
423
425
|
async sample(at) {
|
|
@@ -435,13 +437,16 @@ function createLeftoverTracker(deps) {
|
|
|
435
437
|
const key = keyOf(row.pid, startedAtMs);
|
|
436
438
|
nowLive.set(key, row);
|
|
437
439
|
let entry = ledger.get(key);
|
|
438
|
-
const owner =
|
|
440
|
+
const { owner, walk } = traceOwner(row, byPid, msys, sessions);
|
|
441
|
+
const follower = isFollowRow(row);
|
|
439
442
|
if (entry === void 0) {
|
|
440
443
|
const shell = parentRowOf(row, byPid, msys);
|
|
444
|
+
if (follower) deps.log(`leftover: first sight of follower pid ${row.pid} ${row.name} (${shortCommand(row.commandLine)}), started ${row.startedAt.toISOString()}: launching shell ${shell === void 0 ? "not running" : `pid ${shell.pid} ${shell.name}`}; owner ${owner ?? "none"}, chain ${walk}`);
|
|
441
445
|
entry = { pid: row.pid, startedAtMs, owner, envChecked: false, shellKey: shell?.startedAt ? keyOf(shell.pid, shell.startedAt.getTime()) : null, unreachable: false, lastCpu100ns: null, lastSeenMs: nowMs, hotSinceMs: null, hotCpuStart100ns: null, hotStartWallMs: null, lastCpuPercent: null, remindedAtMs: null };
|
|
442
446
|
ledger.set(key, entry);
|
|
443
447
|
} else if (entry.owner === null && owner !== null) {
|
|
444
448
|
entry.owner = owner;
|
|
449
|
+
if (follower) deps.log(`leftover: follower pid ${row.pid} now has owner ${owner} (chain ${walk})`);
|
|
445
450
|
}
|
|
446
451
|
if (row.cpuTime100ns !== null && entry.lastCpu100ns !== null && nowMs > entry.lastSeenMs) {
|
|
447
452
|
const fraction = (row.cpuTime100ns - entry.lastCpu100ns) / 1e4 / (nowMs - entry.lastSeenMs);
|
|
@@ -459,8 +464,17 @@ function createLeftoverTracker(deps) {
|
|
|
459
464
|
entry.lastCpu100ns = row.cpuTime100ns;
|
|
460
465
|
entry.lastSeenMs = nowMs;
|
|
461
466
|
}
|
|
462
|
-
for (const key of [...ledger
|
|
467
|
+
for (const [key, entry] of [...ledger]) {
|
|
468
|
+
if (nowLive.has(key)) continue;
|
|
469
|
+
if (live.has(key) && isFollowRow(live.get(key))) deps.log(`leftover: follower pid ${entry.pid} ended (owner ${entry.owner ?? "none"})`);
|
|
470
|
+
ledger.delete(key);
|
|
471
|
+
}
|
|
463
472
|
live = nowLive;
|
|
473
|
+
const sessionIds = [...liveSessionIds].sort().join(",");
|
|
474
|
+
if (sessionIds !== lastSessionIds) {
|
|
475
|
+
deps.log(`leftover: watching ${liveSessionIds.size} claude sessions: ${sessionIds}`);
|
|
476
|
+
lastSessionIds = sessionIds;
|
|
477
|
+
}
|
|
464
478
|
const unknown = [...ledger.entries()].filter(([, e]) => e.owner === null && !e.envChecked && e.hotSinceMs !== null && nowMs - e.hotSinceMs >= HOT_MIN_MS);
|
|
465
479
|
if (unknown.length > 0 && nowMs >= envRetryAfterMs) {
|
|
466
480
|
let fromEnv;
|
|
@@ -473,6 +487,7 @@ function createLeftoverTracker(deps) {
|
|
|
473
487
|
for (const [, e] of unknown) {
|
|
474
488
|
e.envChecked = true;
|
|
475
489
|
e.owner = fromEnv.get(e.pid) ?? null;
|
|
490
|
+
deps.log(`leftover: hot process pid ${e.pid} has no claude.exe ancestor; its environment names ${e.owner ?? "no session"}`);
|
|
476
491
|
}
|
|
477
492
|
}
|
|
478
493
|
},
|
|
@@ -497,15 +512,21 @@ function createLeftoverTracker(deps) {
|
|
|
497
512
|
cpu_percent: hot && entry.lastCpuPercent !== null ? Math.round(entry.lastCpuPercent) : null,
|
|
498
513
|
why: readerGone ? "reader_gone" : follow ? "follow" : "cpu"
|
|
499
514
|
};
|
|
515
|
+
const flagged = `leftover: flagged pid ${entry.pid} ${row.name} (${process2.why}, ${process2.age_seconds} s old, owner ${entry.owner ?? "none"}):`;
|
|
500
516
|
if (entry.owner === null) {
|
|
501
517
|
ownerless.push({ ...process2, reason: "no session found" });
|
|
518
|
+
deps.log(`${flagged} no session found, so it goes to the orphan list`);
|
|
502
519
|
} else if (!liveSessionIds.has(entry.owner)) {
|
|
503
520
|
ownerless.push({ ...process2, reason: "session ended" });
|
|
521
|
+
deps.log(`${flagged} its session has ended, so it goes to the orphan list`);
|
|
504
522
|
} else {
|
|
505
523
|
if (entry.unreachable) ownerless.push({ ...process2, reason: "session not on this dashboard" });
|
|
506
524
|
if (entry.remindedAtMs === null || nowMs - entry.remindedAtMs >= REMIND_INTERVAL_MS) {
|
|
507
525
|
sessions.set(entry.owner, [...sessions.get(entry.owner) ?? [], process2]);
|
|
508
526
|
reminded.set(entry.owner, [...reminded.get(entry.owner) ?? [], key]);
|
|
527
|
+
deps.log(`${flagged} the session is reminded in this report`);
|
|
528
|
+
} else {
|
|
529
|
+
deps.log(`${flagged} the session was reminded ${Math.floor((nowMs - entry.remindedAtMs) / 6e4)} min ago, so not again yet`);
|
|
509
530
|
}
|
|
510
531
|
}
|
|
511
532
|
}
|
|
@@ -519,15 +540,17 @@ function createLeftoverTracker(deps) {
|
|
|
519
540
|
entry.unreachable = undelivered.includes(sessionId);
|
|
520
541
|
if (!entry.unreachable) entry.remindedAtMs = at.getTime();
|
|
521
542
|
}
|
|
543
|
+
deps.log(`leftover: the dashboard ${undelivered.includes(sessionId) ? "has no stream row for" : "took the reminder for"} session ${sessionId} (${keys.length} processes)`);
|
|
522
544
|
}
|
|
523
545
|
}
|
|
524
546
|
};
|
|
525
547
|
}
|
|
526
|
-
function createSystemLeftoverTracker(run = runCommand, platform = process.platform) {
|
|
548
|
+
function createSystemLeftoverTracker(log, run = runCommand, platform = process.platform) {
|
|
527
549
|
if (platform !== "win32") return { sample: async () => {
|
|
528
550
|
}, collect: () => ({ sessions: [], ownerless: [], reminded: /* @__PURE__ */ new Map() }), settle: () => {
|
|
529
551
|
} };
|
|
530
552
|
return createLeftoverTracker({
|
|
553
|
+
log,
|
|
531
554
|
listing: (now) => listProcesses(now, run, platform, void 0, "never"),
|
|
532
555
|
msysParents: (rows) => readMsysParents(rows, run),
|
|
533
556
|
claudeSessions: () => readClaudeSessionFiles(),
|
|
@@ -744,6 +767,7 @@ function createReporter(deps) {
|
|
|
744
767
|
const found = deps.leftovers.collect(startedAt);
|
|
745
768
|
const payload = foldOwnerless(reportPayload(await deps.runHealth()), found.ownerless);
|
|
746
769
|
const reportError = "report_error" in payload ? payload.report_error : null;
|
|
770
|
+
if (found.sessions.length > 0 || found.ownerless.length > 0) deps.log(`report at ${startedAt.toISOString()} carries reminders for ${found.sessions.length} sessions and ${found.ownerless.length} ownerless processes`);
|
|
747
771
|
const sent = await postAsMachine(deps.config, deps.fetchFn, "/api/machines/report", found.sessions.length === 0 ? payload : { ...payload, leftovers: { sessions: found.sessions } });
|
|
748
772
|
let outcome;
|
|
749
773
|
let error = reportError ?? sent.error;
|
|
@@ -808,7 +832,7 @@ function createReporter(deps) {
|
|
|
808
832
|
}
|
|
809
833
|
|
|
810
834
|
// src/machine-daemon/state.ts
|
|
811
|
-
import { chmodSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync3, renameSync, writeFileSync } from "node:fs";
|
|
835
|
+
import { appendFileSync, chmodSync, existsSync as existsSync2, mkdirSync, readFileSync as readFileSync3, renameSync, statSync, writeFileSync } from "node:fs";
|
|
812
836
|
import { homedir as homedir2 } from "node:os";
|
|
813
837
|
import { dirname, join as join3 } from "node:path";
|
|
814
838
|
var MACHINE_DAEMON_DIR_NAME = ".danxbot";
|
|
@@ -838,6 +862,9 @@ function daemonConfigPath(home = danxbotHome()) {
|
|
|
838
862
|
function daemonStatusPath(home = danxbotHome()) {
|
|
839
863
|
return join3(home, "machine-daemon-status.json");
|
|
840
864
|
}
|
|
865
|
+
function daemonLogPath(home = danxbotHome()) {
|
|
866
|
+
return join3(home, "machine-daemon.log");
|
|
867
|
+
}
|
|
841
868
|
function daemonBundleDir(home = danxbotHome()) {
|
|
842
869
|
return join3(home, "machine-daemon");
|
|
843
870
|
}
|
|
@@ -856,6 +883,19 @@ function readJsonIfPresent(file) {
|
|
|
856
883
|
if (!existsSync2(file)) return null;
|
|
857
884
|
return JSON.parse(readFileSync3(file, "utf8"));
|
|
858
885
|
}
|
|
886
|
+
var DAEMON_LOG_MAX_BYTES = 1024 * 1024;
|
|
887
|
+
function createDaemonLog(file, now = () => /* @__PURE__ */ new Date(), maxBytes = DAEMON_LOG_MAX_BYTES) {
|
|
888
|
+
return (line) => {
|
|
889
|
+
try {
|
|
890
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
891
|
+
if (existsSync2(file) && statSync(file).size >= maxBytes) renameSync(file, `${file}.1`);
|
|
892
|
+
appendFileSync(file, `${now().toISOString()} ${line}
|
|
893
|
+
`, "utf8");
|
|
894
|
+
} catch (err) {
|
|
895
|
+
console.error(`[machine-daemon] cannot write ${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
896
|
+
}
|
|
897
|
+
};
|
|
898
|
+
}
|
|
859
899
|
|
|
860
900
|
// src/machine-daemon/run.ts
|
|
861
901
|
function finishBeatStop(stop, disable) {
|
|
@@ -877,7 +917,12 @@ async function runDaemonProcess() {
|
|
|
877
917
|
}
|
|
878
918
|
const fetchFn = fetch;
|
|
879
919
|
const now = () => /* @__PURE__ */ new Date();
|
|
880
|
-
const
|
|
920
|
+
const logToFile = createDaemonLog(daemonLogPath());
|
|
921
|
+
const log = (line) => {
|
|
922
|
+
console.log(`[machine-daemon] ${line}`);
|
|
923
|
+
logToFile(line);
|
|
924
|
+
};
|
|
925
|
+
log(`started: pid ${process.pid}, daemon ${config.daemon_version}, dashboard ${config.dashboard_url}`);
|
|
881
926
|
const status = createStatusWriter((value) => writeOwnerOnlyJson(daemonStatusPath(), value));
|
|
882
927
|
const stop = await runBeatLoop({
|
|
883
928
|
config,
|
|
@@ -894,7 +939,7 @@ async function runDaemonProcess() {
|
|
|
894
939
|
config,
|
|
895
940
|
runHealth: () => runHealthChild({ spawnFn: spawn, nodePath: process.execPath, toolkitPath: toolkitBundlePath() }),
|
|
896
941
|
fetchFn,
|
|
897
|
-
leftovers: createSystemLeftoverTracker(),
|
|
942
|
+
leftovers: createSystemLeftoverTracker(log),
|
|
898
943
|
writeStatus: status.report,
|
|
899
944
|
log,
|
|
900
945
|
installed: () => existsSync3(daemonConfigPath())
|
package/dist/machine-toolkit.js
CHANGED
|
@@ -443,7 +443,7 @@ function resolveToolkitVersion(stamped, readManifest) {
|
|
|
443
443
|
return stamped ?? readManifest();
|
|
444
444
|
}
|
|
445
445
|
var readOwnManifest = () => JSON.parse(readFileSync2(new URL("../../package.json", import.meta.url), "utf8")).version;
|
|
446
|
-
var TOOLKIT_VERSION = resolveToolkitVersion(false ? void 0 : "0.1.
|
|
446
|
+
var TOOLKIT_VERSION = resolveToolkitVersion(false ? void 0 : "0.1.459", readOwnManifest);
|
|
447
447
|
function envelope(now) {
|
|
448
448
|
return { contract_version: CONTRACT_VERSION, toolkit_version: TOOLKIT_VERSION, profile: PROFILE, measured_at: now.toISOString() };
|
|
449
449
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@50arms/platform-mcp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.459",
|
|
4
4
|
"description": "Stdio MCP server fronting the 50 Arms dashboard API for agents (operator sessions and dispatched workers alike): the generic 50arms_api/50arms_api_list/50arms_api_spec tools reach any route the caller's credential may call, plus dedicated agent-specific tools (session_connect, request_permission, attach_file, and 50arms_slack_reply) (DX-3465, PLN-11 R-25).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|