@50arms/platform-mcp 0.1.482 → 0.1.484
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/app-version.js +24 -0
- package/dist/http-client.js +64 -2
- package/dist/index.js +6 -1
- package/dist/machine-daemon/beat.js +3 -0
- package/dist/machine-daemon/cli.js +13 -1
- package/dist/machine-daemon/install.js +28 -4
- package/dist/machine-daemon/state.js +4 -0
- package/dist/machine-daemon.js +197 -102
- package/dist/machine-toolkit.js +1 -1
- package/dist/result-limit.js +3 -0
- package/dist/route-board-scope.generated.js +1 -1
- package/package.json +2 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DX-7767 - the dashboard's app version, `<major>.<minor>.<patch>`, which it sends on every response as the header `X-50Arms-Version`. The plugin cannot see HTTP headers (every answer reaches it as the
|
|
3
|
+
* `{ok, status, body}` envelope), so the client copies the header into the envelope as `app_version`, on every `50arms_api` answer and on `session_connect`'s. A response with no header carries no
|
|
4
|
+
* `app_version` key, and a value that is not a version is dropped, never forwarded: the plugin compares the number with the one in the agent's own checkout (`50ARMS_VERSION`) and tells the agent to rebase
|
|
5
|
+
* onto main when the checkout is older by a minor or a major version.
|
|
6
|
+
*/
|
|
7
|
+
/** Must match `APP_VERSION_HEADER` in danxbot's `src/version/app-version.ts` verbatim. */
|
|
8
|
+
export const APP_VERSION_HEADER = "X-50Arms-Version";
|
|
9
|
+
/** Three decimal integers, no sign, no leading zeros, no suffix: `APP_VERSION_PATTERN` in danxbot's `src/version/app-version.ts`. */
|
|
10
|
+
const APP_VERSION_PATTERN = /^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$/;
|
|
11
|
+
/** The app version the response's header carries, or `undefined` when it has none or holds anything but a version. */
|
|
12
|
+
export function appVersionOf(headers) {
|
|
13
|
+
const value = headers.get(APP_VERSION_HEADER);
|
|
14
|
+
return value !== null && APP_VERSION_PATTERN.test(value) ? value : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** `body` with the answer's `app_version` added when it has one and `body` is a plain object; any other body is returned as it is. */
|
|
17
|
+
export function withAppVersion(body, answer) {
|
|
18
|
+
if (answer.app_version === undefined ||
|
|
19
|
+
typeof body !== "object" ||
|
|
20
|
+
body === null ||
|
|
21
|
+
Array.isArray(body))
|
|
22
|
+
return body;
|
|
23
|
+
return { ...body, app_version: answer.app_version };
|
|
24
|
+
}
|
package/dist/http-client.js
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thin HTTP client for the danxbot dashboard's `/api/issues/*` surface (and,
|
|
3
|
+
* via `RequestArgs.basePath`, a sibling board-scoped surface such as
|
|
4
|
+
* `/api/quality-gates`).
|
|
5
|
+
*
|
|
6
|
+
* Responsibilities (and ONLY these — anything richer belongs in the
|
|
7
|
+
* per-route handler):
|
|
8
|
+
*
|
|
9
|
+
* - Build `<baseUrl><basePath><path>` and, where the dashboard takes it
|
|
10
|
+
* (`boardStampOf`, `./route-board-scope.ts`), append `?board=<board>` —
|
|
11
|
+
* `RequestArgs.board` overrides the configured dispatch board (a
|
|
12
|
+
* board ref) for a single call, enabling cross-board
|
|
13
|
+
* reads/writes from any dispatch. DX-5337: a route that reads its board
|
|
14
|
+
* from the body only (`takesBoardInBody`: issue create and import) gets
|
|
15
|
+
* that board in `body.board` instead, never in the query. The dashboard owns board→repo
|
|
16
|
+
* resolution; this package never sends repo. Existing query params on
|
|
17
|
+
* `path` are preserved. `basePath` defaults to `/api/issues` (every
|
|
18
|
+
* original caller) — a handler targeting a different route family (e.g.
|
|
19
|
+
* `/api/quality-gates`) passes it explicitly so the URL is never
|
|
20
|
+
* silently built against the wrong base.
|
|
21
|
+
*
|
|
22
|
+
* DX-3750 — `board` used to be stamped on EVERY request regardless of
|
|
23
|
+
* route, and `50arms_api` (the one generic tool, DX-3465) forwards it to
|
|
24
|
+
* an arbitrary caller-supplied path. The dashboard answers 400 to a query
|
|
25
|
+
* key a strict route does not take: a list read whose vocabulary omits
|
|
26
|
+
* `board` (plans, dispatches, boards, ...) and the single-board read.
|
|
27
|
+
* DX-4303 — which routes those are is the dashboard's own route table,
|
|
28
|
+
* shipped in this package as a generated module and matched by
|
|
29
|
+
* `boardStampOf`; the hand-kept prefix list this client carried
|
|
30
|
+
* (DX-3750, DX-4261) is gone, so a route that gains or loses `board`
|
|
31
|
+
* changes one definition on the server.
|
|
32
|
+
* - Stamp `Authorization: Bearer <token>` on every request, plus
|
|
33
|
+
* `Content-Type: application/json` when a body is sent.
|
|
34
|
+
* - Pass the response through verbatim — 2xx → `{ok: true, body}`,
|
|
35
|
+
* 4xx → `{ok: false, body}`; a request that gets no usable answer throws fail-loud (DX-7110, below).
|
|
36
|
+
* - DX-7110 (DEC-736) - every attempt is bounded by `boundRequest` (`./request-policy.ts`: a read 30 s, a write 120 s, the same
|
|
37
|
+
* bounds for this process's own GET and POST). A READ the agent made (a GET whose route does not write, `isReadRoute`) that fails in transit
|
|
38
|
+
* (nothing connected, a timeout, 502/503/504, 408/429) is retried with backoff for up to `readWindowMs`; a write is sent once,
|
|
39
|
+
* because its first send may have landed. A request that gets no usable answer throws `DashboardUnreachableError` (carrying whether
|
|
40
|
+
* a write can have been applied) and a 500 or other 5xx throws `DashboardServerError` at once (never retried, never an outage);
|
|
41
|
+
* `failureAnswer` (`./dashboard-errors.ts`) turns them into the answer an agent sees. Once retries run out `reachability` records the
|
|
42
|
+
* outage and later calls make one short attempt (`./dashboard-reachability.ts`).
|
|
43
|
+
* - DX-3597 — on the FIRST call this instance makes (any status), and on
|
|
44
|
+
* every subsequent 4xx, compare the `X-Danx-Mcp-Min-Version` response
|
|
45
|
+
* header against this package's own version (`./version-floor.ts`) and
|
|
46
|
+
* throw `McpOutdatedError` instead of returning the envelope when this
|
|
47
|
+
* package is behind — the same "throw fail-loud" contract as a
|
|
48
|
+
* 5xx/network failure, because an outdated MCP is exactly that class of
|
|
49
|
+
* failure. See `version-floor.ts`'s own module doc for the full
|
|
50
|
+
* rationale.
|
|
51
|
+
*
|
|
52
|
+
* DX-7767: the envelope also carries the dashboard's app version as `app_version` (the `X-50Arms-Version` header, `app-version.ts`), because the plugin sees no HTTP headers.
|
|
53
|
+
*
|
|
54
|
+
* The envelope passthrough is load-bearing: the server's `V2Error`
|
|
55
|
+
* refusals carry structured detail (e.g. `failed_gate`, `non_terminal_phases`,
|
|
56
|
+
* `offending_keys`) that the agent needs to pick the right next action.
|
|
57
|
+
* Transforming the body here would erase that signal.
|
|
58
|
+
*/
|
|
59
|
+
import { appVersionOf } from "./app-version.js";
|
|
1
60
|
import { boardStampOf, isReadRoute, takesBoardInBody, } from "./route-board-scope.js";
|
|
2
61
|
import { assertVersionFloorSatisfied, MCP_MIN_VERSION_HEADER, MCP_VERSION_HEADER, readOwnVersion, } from "./version-floor.js";
|
|
3
62
|
import { PLUGIN_BACKGROUND_HEADER } from "./session-options.js";
|
|
@@ -278,10 +337,13 @@ export class DashboardHttpClient {
|
|
|
278
337
|
throw new Error(`[platform-mcp] non-JSON response (status ${res.status}) from ${method} ${url}: ${text.slice(0, 200)}`);
|
|
279
338
|
}
|
|
280
339
|
}
|
|
340
|
+
// DX-7767: the plugin sees no HTTP headers, so the app version rides the envelope.
|
|
341
|
+
const appVersion = appVersionOf(res.headers);
|
|
342
|
+
const version = appVersion === undefined ? {} : { app_version: appVersion };
|
|
281
343
|
if (res.status >= 200 && res.status < 300) {
|
|
282
|
-
return { ok: true, status: res.status, body: parsed };
|
|
344
|
+
return { ok: true, status: res.status, ...version, body: parsed };
|
|
283
345
|
}
|
|
284
|
-
return { ok: false, status: res.status, body: parsed };
|
|
346
|
+
return { ok: false, status: res.status, ...version, body: parsed };
|
|
285
347
|
}
|
|
286
348
|
/**
|
|
287
349
|
* DX-7110 - one `GET /health` attempt, for the reachability ping: whether 50 Arms answered healthy. The probe is public (no bearer) and
|
package/dist/index.js
CHANGED
|
@@ -109,6 +109,7 @@ import { docsRead, docsSearch, docsSourceShape } from "./docs-tools.js";
|
|
|
109
109
|
import { spillRoleInstruction } from "./role-instruction-file.js";
|
|
110
110
|
import { DEFAULT_LIMIT, MAX_SEARCH_LIMIT } from "@50arms/docs/agent";
|
|
111
111
|
import { boundedResultText } from "./result-limit.js";
|
|
112
|
+
import { withAppVersion } from "./app-version.js";
|
|
112
113
|
import { bodyValueSchema, queryValueSchema } from "./types.js";
|
|
113
114
|
import { termsAcceptanceCallHalt, termsHaltFor } from "./terms-halt.js";
|
|
114
115
|
import { danxbotApiExamplesText } from "./danxbot-api-examples.js";
|
|
@@ -1126,6 +1127,10 @@ async (args) => {
|
|
|
1126
1127
|
void reportCheckoutDocsRoot();
|
|
1127
1128
|
return jsonResult({
|
|
1128
1129
|
...claimed.body,
|
|
1130
|
+
// DX-7767: the sign-in reply is the answer's body, so the envelope's app version is carried here.
|
|
1131
|
+
...(claimed.app_version === undefined
|
|
1132
|
+
? {}
|
|
1133
|
+
: { app_version: claimed.app_version }),
|
|
1129
1134
|
...(agentGate.current() === null
|
|
1130
1135
|
? {
|
|
1131
1136
|
instruction: "This session is signed in. Call `session_connect` with `target` to connect it to a plan or a card.",
|
|
@@ -1159,7 +1164,7 @@ async function finishConnect(tool, result, disconnect) {
|
|
|
1159
1164
|
if (!disconnect) {
|
|
1160
1165
|
agentGate.record(result.body);
|
|
1161
1166
|
if (agentGate.current() !== null)
|
|
1162
|
-
return jsonResult(result.body);
|
|
1167
|
+
return jsonResult(withAppVersion(result.body, result));
|
|
1163
1168
|
void reportCheckoutDocsRoot();
|
|
1164
1169
|
}
|
|
1165
1170
|
if (disconnect) {
|
|
@@ -83,6 +83,9 @@ export async function runBeatLoop(deps) {
|
|
|
83
83
|
}
|
|
84
84
|
if (deps.reporter.refused())
|
|
85
85
|
return stopOnRefusal(deps);
|
|
86
|
+
// DX-7825: the daemon replaced its own bundles; this process is the old one and ends so the start entry runs the new one.
|
|
87
|
+
if (deps.reporter.refreshed())
|
|
88
|
+
return { reason: "refreshed" };
|
|
86
89
|
void deps.reporter.sample(beatAt);
|
|
87
90
|
deps.reporter.tick(beatAt);
|
|
88
91
|
if (deps.maxTicks !== undefined && ticks >= deps.maxTicks)
|
|
@@ -4,11 +4,12 @@
|
|
|
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, refreshDaemon, uninstallDaemon, } from "./install.js";
|
|
8
8
|
import { productionAutostartDeps } from "./autostart.js";
|
|
9
9
|
import { daemonBundleDir, danxbotHome } from "./state.js";
|
|
10
10
|
export const MACHINE_DAEMON_SUBCOMMANDS = [
|
|
11
11
|
"install-daemon",
|
|
12
|
+
"refresh-daemon",
|
|
12
13
|
"status",
|
|
13
14
|
"uninstall-daemon",
|
|
14
15
|
];
|
|
@@ -39,6 +40,17 @@ export async function runMachineDaemonCommand(subcommand, args) {
|
|
|
39
40
|
}));
|
|
40
41
|
return 0;
|
|
41
42
|
}
|
|
43
|
+
if (subcommand === "refresh-daemon") {
|
|
44
|
+
// DX-7825: the daemon runs this itself (refresh.ts) through npx @latest; the bundles it copies are this package's own.
|
|
45
|
+
const dist = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
46
|
+
const bundlePath = refreshDaemon({
|
|
47
|
+
home,
|
|
48
|
+
bundleSource: join(dist, "machine-daemon.js"),
|
|
49
|
+
toolkitBundleSource: join(dist, "machine-toolkit.js"),
|
|
50
|
+
});
|
|
51
|
+
console.log(`Refreshed the daemon bundles under ${bundlePath}.`);
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
42
54
|
if (subcommand === "uninstall-daemon") {
|
|
43
55
|
const autostart = productionAutostartDeps(join(daemonBundleDir(home), "machine-daemon.js"));
|
|
44
56
|
await uninstallDaemon({
|
|
@@ -135,6 +135,33 @@ async function registerWorkerHost(deps) {
|
|
|
135
135
|
bearer: await registerWithKey(deps, machineId, machineKey),
|
|
136
136
|
};
|
|
137
137
|
}
|
|
138
|
+
/** Copies the two bundles under `<home>/machine-daemon/` and returns the daemon bundle's path. The one copy both `install-daemon` and `refresh-daemon` make. */
|
|
139
|
+
function copyDaemonBundles(deps) {
|
|
140
|
+
mkdirSync(daemonBundleDir(deps.home), { recursive: true });
|
|
141
|
+
const bundlePath = join(daemonBundleDir(deps.home), "machine-daemon.js");
|
|
142
|
+
copyFileSync(deps.bundleSource, bundlePath);
|
|
143
|
+
copyFileSync(deps.toolkitBundleSource, toolkitBundlePath(deps.home));
|
|
144
|
+
return bundlePath;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* DX-7825: `refresh-daemon`, run by the daemon itself (`refresh.ts`) from the newest published package when the dashboard needs a newer machine-toolkit contract than the installed bundles carry. It replaces the two bundles only: it
|
|
148
|
+
* registers nothing and keeps the bearer, so no person is involved. The daemon that started it ends afterwards and the start entry runs the new bundle.
|
|
149
|
+
*/
|
|
150
|
+
export function refreshDaemon(deps) {
|
|
151
|
+
const config = readJsonIfPresent(daemonConfigPath(deps.home));
|
|
152
|
+
if (config === null)
|
|
153
|
+
throw new Error("no daemon is installed here: run npx @50arms/platform-mcp install-daemon");
|
|
154
|
+
for (const source of [deps.bundleSource, deps.toolkitBundleSource]) {
|
|
155
|
+
if (!existsSync(source))
|
|
156
|
+
throw new Error(`this package has no ${source}: it cannot refresh the daemon`);
|
|
157
|
+
}
|
|
158
|
+
const bundlePath = copyDaemonBundles(deps);
|
|
159
|
+
writeOwnerOnlyJson(daemonConfigPath(deps.home), {
|
|
160
|
+
...config,
|
|
161
|
+
daemon_version: DAEMON_VERSION,
|
|
162
|
+
});
|
|
163
|
+
return bundlePath;
|
|
164
|
+
}
|
|
138
165
|
export async function installDaemon(deps) {
|
|
139
166
|
// DX-6537: checked before registering, which replaces the bearer of a daemon already running here: a half-installed package changes nothing.
|
|
140
167
|
for (const source of [deps.bundleSource, deps.toolkitBundleSource]) {
|
|
@@ -157,10 +184,7 @@ export async function installDaemon(deps) {
|
|
|
157
184
|
daemon_version: DAEMON_VERSION,
|
|
158
185
|
};
|
|
159
186
|
writeOwnerOnlyJson(daemonConfigPath(deps.home), config);
|
|
160
|
-
|
|
161
|
-
const bundlePath = join(daemonBundleDir(deps.home), "machine-daemon.js");
|
|
162
|
-
copyFileSync(deps.bundleSource, bundlePath);
|
|
163
|
-
copyFileSync(deps.toolkitBundleSource, toolkitBundlePath(deps.home));
|
|
187
|
+
const bundlePath = copyDaemonBundles(deps);
|
|
164
188
|
const entry = installAutostart({ ...deps.autostart, bundlePath });
|
|
165
189
|
deps.print(`Registered machine ${machineId} as "${deps.label}". The daemon runs from ${bundlePath}.`);
|
|
166
190
|
deps.print(`Start at logon: ${entry}.`);
|
|
@@ -7,6 +7,7 @@
|
|
|
7
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; owner-only (0600), since a flagged line carries a process's command line.
|
|
8
8
|
* machine-daemon-status.json — written after every beat and report: when it last beat, whether that beat landed, when the next is
|
|
9
9
|
* due, and the last health report's time, outcome and error.
|
|
10
|
+
* machine-daemon-refresh.json — DX-7825: the last time the daemon ran `refresh-daemon` for a contract the dashboard needed (so a package that cannot satisfy it is not retried in a loop).
|
|
10
11
|
* machine-daemon/ — the copies of the daemon and toolkit bundles the autostart entry runs (so a package update never changes a running file).
|
|
11
12
|
*/
|
|
12
13
|
import { appendFileSync, chmodSync, existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync, } from "node:fs";
|
|
@@ -43,6 +44,9 @@ export function daemonConfigPath(home = danxbotHome()) {
|
|
|
43
44
|
export function daemonStatusPath(home = danxbotHome()) {
|
|
44
45
|
return join(home, "machine-daemon-status.json");
|
|
45
46
|
}
|
|
47
|
+
export function daemonRefreshMarkerPath(home = danxbotHome()) {
|
|
48
|
+
return join(home, "machine-daemon-refresh.json");
|
|
49
|
+
}
|
|
46
50
|
export function daemonLogPath(home = danxbotHome()) {
|
|
47
51
|
return join(home, "machine-daemon.log");
|
|
48
52
|
}
|
package/dist/machine-daemon.js
CHANGED
|
@@ -252,6 +252,7 @@ async function runBeatLoop(deps) {
|
|
|
252
252
|
);
|
|
253
253
|
}
|
|
254
254
|
if (deps.reporter.refused()) return stopOnRefusal(deps);
|
|
255
|
+
if (deps.reporter.refreshed()) return { reason: "refreshed" };
|
|
255
256
|
void deps.reporter.sample(beatAt);
|
|
256
257
|
deps.reporter.tick(beatAt);
|
|
257
258
|
if (deps.maxTicks !== void 0 && ticks >= deps.maxTicks)
|
|
@@ -840,6 +841,179 @@ function createSystemLeftoverTracker(log, run = runCommand, platform = process.p
|
|
|
840
841
|
});
|
|
841
842
|
}
|
|
842
843
|
|
|
844
|
+
// src/machine-toolkit/deadline.ts
|
|
845
|
+
var realScheduler = (fire, ms) => {
|
|
846
|
+
const timer = setTimeout(fire, ms);
|
|
847
|
+
return () => clearTimeout(timer);
|
|
848
|
+
};
|
|
849
|
+
|
|
850
|
+
// src/machine-daemon/state.ts
|
|
851
|
+
import {
|
|
852
|
+
appendFileSync,
|
|
853
|
+
chmodSync,
|
|
854
|
+
existsSync as existsSync2,
|
|
855
|
+
mkdirSync,
|
|
856
|
+
readFileSync as readFileSync3,
|
|
857
|
+
renameSync,
|
|
858
|
+
statSync,
|
|
859
|
+
writeFileSync
|
|
860
|
+
} from "node:fs";
|
|
861
|
+
import { homedir as homedir2 } from "node:os";
|
|
862
|
+
import { dirname, join as join2 } from "node:path";
|
|
863
|
+
var MACHINE_DAEMON_DIR_NAME = ".danxbot";
|
|
864
|
+
function createStatusWriter(write) {
|
|
865
|
+
let current = {
|
|
866
|
+
pid: process.pid,
|
|
867
|
+
last_beat_at: null,
|
|
868
|
+
last_beat_ok: null,
|
|
869
|
+
last_error: null,
|
|
870
|
+
next_beat_at: null,
|
|
871
|
+
last_report_at: null,
|
|
872
|
+
last_report_outcome: null,
|
|
873
|
+
last_report_error: null
|
|
874
|
+
};
|
|
875
|
+
const merge = (fields) => {
|
|
876
|
+
current = { ...current, ...fields };
|
|
877
|
+
write(current);
|
|
878
|
+
};
|
|
879
|
+
return { beat: merge, report: merge };
|
|
880
|
+
}
|
|
881
|
+
function danxbotHome() {
|
|
882
|
+
return join2(homedir2(), MACHINE_DAEMON_DIR_NAME);
|
|
883
|
+
}
|
|
884
|
+
function daemonConfigPath(home = danxbotHome()) {
|
|
885
|
+
return join2(home, "machine-daemon.json");
|
|
886
|
+
}
|
|
887
|
+
function daemonStatusPath(home = danxbotHome()) {
|
|
888
|
+
return join2(home, "machine-daemon-status.json");
|
|
889
|
+
}
|
|
890
|
+
function daemonRefreshMarkerPath(home = danxbotHome()) {
|
|
891
|
+
return join2(home, "machine-daemon-refresh.json");
|
|
892
|
+
}
|
|
893
|
+
function daemonLogPath(home = danxbotHome()) {
|
|
894
|
+
return join2(home, "machine-daemon.log");
|
|
895
|
+
}
|
|
896
|
+
function daemonBundleDir(home = danxbotHome()) {
|
|
897
|
+
return join2(home, "machine-daemon");
|
|
898
|
+
}
|
|
899
|
+
function toolkitBundlePath(home = danxbotHome()) {
|
|
900
|
+
return join2(daemonBundleDir(home), "machine-toolkit.js");
|
|
901
|
+
}
|
|
902
|
+
function writeOwnerOnlyJson(file, value) {
|
|
903
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
904
|
+
const tmp = `${file}.${process.pid}.tmp`;
|
|
905
|
+
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}
|
|
906
|
+
`, {
|
|
907
|
+
encoding: "utf8",
|
|
908
|
+
mode: 384
|
|
909
|
+
});
|
|
910
|
+
if (process.platform !== "win32") chmodSync(tmp, 384);
|
|
911
|
+
renameSync(tmp, file);
|
|
912
|
+
}
|
|
913
|
+
function readJsonIfPresent(file) {
|
|
914
|
+
if (!existsSync2(file)) return null;
|
|
915
|
+
return JSON.parse(readFileSync3(file, "utf8"));
|
|
916
|
+
}
|
|
917
|
+
var DAEMON_LOG_MAX_BYTES = 1024 * 1024;
|
|
918
|
+
function createDaemonLog(file, now = () => /* @__PURE__ */ new Date(), maxBytes = DAEMON_LOG_MAX_BYTES) {
|
|
919
|
+
let narrowed = false;
|
|
920
|
+
return (line) => {
|
|
921
|
+
try {
|
|
922
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
923
|
+
if (!narrowed && existsSync2(file) && process.platform !== "win32")
|
|
924
|
+
chmodSync(file, 384);
|
|
925
|
+
narrowed = true;
|
|
926
|
+
if (existsSync2(file) && statSync(file).size >= maxBytes)
|
|
927
|
+
renameSync(file, `${file}.1`);
|
|
928
|
+
appendFileSync(file, `${now().toISOString()} ${line}
|
|
929
|
+
`, {
|
|
930
|
+
encoding: "utf8",
|
|
931
|
+
mode: 384
|
|
932
|
+
});
|
|
933
|
+
} catch (err) {
|
|
934
|
+
console.error(
|
|
935
|
+
`[machine-daemon] cannot write ${file}: ${err instanceof Error ? err.message : String(err)}`
|
|
936
|
+
);
|
|
937
|
+
}
|
|
938
|
+
};
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// src/machine-daemon/refresh.ts
|
|
942
|
+
var REFRESH_RETRY_MS = 6 * 60 * 60 * 1e3;
|
|
943
|
+
var REFRESH_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
944
|
+
function refreshDue(marker, needs, now) {
|
|
945
|
+
if (marker === null || marker.needs_contract !== needs) return true;
|
|
946
|
+
return now.getTime() - Date.parse(marker.attempted_at) >= REFRESH_RETRY_MS;
|
|
947
|
+
}
|
|
948
|
+
function runRefreshChild(opts) {
|
|
949
|
+
return new Promise((resolve) => {
|
|
950
|
+
const schedule = opts.schedule ?? realScheduler;
|
|
951
|
+
let settled = false;
|
|
952
|
+
let cancel = () => {
|
|
953
|
+
};
|
|
954
|
+
const finish = (result) => {
|
|
955
|
+
if (settled) return;
|
|
956
|
+
settled = true;
|
|
957
|
+
cancel();
|
|
958
|
+
resolve(result);
|
|
959
|
+
};
|
|
960
|
+
const child = opts.spawnFn(
|
|
961
|
+
"npx",
|
|
962
|
+
["-y", "@50arms/platform-mcp@latest", "refresh-daemon"],
|
|
963
|
+
{
|
|
964
|
+
windowsHide: true,
|
|
965
|
+
shell: process.platform === "win32",
|
|
966
|
+
stdio: "ignore"
|
|
967
|
+
}
|
|
968
|
+
);
|
|
969
|
+
cancel = schedule(() => {
|
|
970
|
+
child.kill();
|
|
971
|
+
finish({ ok: false, reason: "npx did not finish within 5 minutes" });
|
|
972
|
+
}, opts.timeoutMs ?? REFRESH_TIMEOUT_MS);
|
|
973
|
+
child.on(
|
|
974
|
+
"error",
|
|
975
|
+
(err) => finish({ ok: false, reason: `npx could not start (${errorCode(err)})` })
|
|
976
|
+
);
|
|
977
|
+
child.on(
|
|
978
|
+
"close",
|
|
979
|
+
(code) => finish(
|
|
980
|
+
code === 0 ? { ok: true } : {
|
|
981
|
+
ok: false,
|
|
982
|
+
reason: `refresh-daemon exited ${code ?? "on a signal"}`
|
|
983
|
+
}
|
|
984
|
+
)
|
|
985
|
+
);
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
async function refreshBundles(opts) {
|
|
989
|
+
const markerPath = opts.markerPath ?? daemonRefreshMarkerPath();
|
|
990
|
+
let marker = null;
|
|
991
|
+
try {
|
|
992
|
+
marker = readJsonIfPresent(markerPath);
|
|
993
|
+
} catch (err) {
|
|
994
|
+
opts.log(
|
|
995
|
+
`the refresh marker ${markerPath} is unreadable (${errMessage(err)}): refreshing anyway`
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
if (!refreshDue(marker, opts.needs, opts.now)) return false;
|
|
999
|
+
writeOwnerOnlyJson(markerPath, {
|
|
1000
|
+
needs_contract: opts.needs,
|
|
1001
|
+
attempted_at: opts.now.toISOString()
|
|
1002
|
+
});
|
|
1003
|
+
opts.log(
|
|
1004
|
+
`the dashboard needs machine-toolkit contract ${opts.needs}, newer than this daemon's: refreshing the bundles from the newest published package`
|
|
1005
|
+
);
|
|
1006
|
+
const result = await runRefreshChild({ spawnFn: opts.spawnFn });
|
|
1007
|
+
if (!result.ok) {
|
|
1008
|
+
opts.log(
|
|
1009
|
+
`the bundle refresh failed: ${result.reason}; the next attempt is in 6 hours, or run npx @50arms/platform-mcp install-daemon`
|
|
1010
|
+
);
|
|
1011
|
+
return false;
|
|
1012
|
+
}
|
|
1013
|
+
opts.log("the bundles were refreshed: this daemon ends so the new one runs");
|
|
1014
|
+
return true;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
843
1017
|
// src/machine-toolkit/contract.ts
|
|
844
1018
|
var CONTRACT_VERSION = 3;
|
|
845
1019
|
var REQUIRED_METRICS = [
|
|
@@ -857,20 +1031,20 @@ var ContractViolation = class extends Error {
|
|
|
857
1031
|
}
|
|
858
1032
|
};
|
|
859
1033
|
var isObject = (v) => v !== null && typeof v === "object" && !Array.isArray(v);
|
|
860
|
-
var
|
|
1034
|
+
var join3 = (path, key) => path === "" ? key : `${path}.${key}`;
|
|
861
1035
|
function objectAt(v, path, fail) {
|
|
862
1036
|
if (!isObject(v)) return fail(path, "must be an object");
|
|
863
1037
|
return v;
|
|
864
1038
|
}
|
|
865
1039
|
function exactKeys(o, path, keys, fail) {
|
|
866
|
-
for (const k of keys) if (!(k in o)) fail(
|
|
1040
|
+
for (const k of keys) if (!(k in o)) fail(join3(path, k), "is required");
|
|
867
1041
|
for (const k of Object.keys(o))
|
|
868
|
-
if (!keys.includes(k)) fail(
|
|
1042
|
+
if (!keys.includes(k)) fail(join3(path, k), "is not part of the contract");
|
|
869
1043
|
}
|
|
870
1044
|
function stringAt(o, key, path, fail) {
|
|
871
1045
|
const v = o[key];
|
|
872
1046
|
if (typeof v !== "string" || v === "")
|
|
873
|
-
return fail(
|
|
1047
|
+
return fail(join3(path, key), "must be a non-empty string");
|
|
874
1048
|
return v;
|
|
875
1049
|
}
|
|
876
1050
|
function listAt(v, path, fail) {
|
|
@@ -882,7 +1056,7 @@ function checkItem(v, path, extra, fail) {
|
|
|
882
1056
|
exactKeys(o, path, ["id", "detail", ...extra], fail);
|
|
883
1057
|
stringAt(o, "id", path, fail);
|
|
884
1058
|
if (typeof o.detail !== "string")
|
|
885
|
-
fail(
|
|
1059
|
+
fail(join3(path, "detail"), "must be a string");
|
|
886
1060
|
for (const k of extra) stringAt(o, k, path, fail);
|
|
887
1061
|
}
|
|
888
1062
|
function checkItems(v, path, extra, fail) {
|
|
@@ -973,7 +1147,7 @@ var VALIDATORS = {
|
|
|
973
1147
|
if (step.status !== "done" && step.status !== "skipped" && step.status !== "failed")
|
|
974
1148
|
fail(`${path}.status`, "must be 'done', 'skipped' or 'failed'");
|
|
975
1149
|
if (typeof step.detail !== "string")
|
|
976
|
-
fail(
|
|
1150
|
+
fail(join3(path, "detail"), "must be a string");
|
|
977
1151
|
});
|
|
978
1152
|
},
|
|
979
1153
|
"label-check"(o, fail) {
|
|
@@ -993,12 +1167,6 @@ function assertContract(command, report) {
|
|
|
993
1167
|
VALIDATORS[command](objectAt(report, "", fail), fail);
|
|
994
1168
|
}
|
|
995
1169
|
|
|
996
|
-
// src/machine-toolkit/deadline.ts
|
|
997
|
-
var realScheduler = (fire, ms) => {
|
|
998
|
-
const timer = setTimeout(fire, ms);
|
|
999
|
-
return () => clearTimeout(timer);
|
|
1000
|
-
};
|
|
1001
|
-
|
|
1002
1170
|
// src/machine-daemon/report.ts
|
|
1003
1171
|
var REPORT_INTERVAL_MS = 5 * 60 * 1e3;
|
|
1004
1172
|
var REPORT_TICK_TOLERANCE_MS = 1e3;
|
|
@@ -1107,6 +1275,14 @@ function answerOf(text) {
|
|
|
1107
1275
|
return null;
|
|
1108
1276
|
}
|
|
1109
1277
|
}
|
|
1278
|
+
function needsContractOf(text) {
|
|
1279
|
+
try {
|
|
1280
|
+
const { contract_version } = JSON.parse(text);
|
|
1281
|
+
return typeof contract_version === "number" ? contract_version : null;
|
|
1282
|
+
} catch {
|
|
1283
|
+
return null;
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1110
1286
|
function storedKind(text) {
|
|
1111
1287
|
try {
|
|
1112
1288
|
const { stored } = JSON.parse(text);
|
|
@@ -1120,6 +1296,7 @@ function createReporter(deps) {
|
|
|
1120
1296
|
let running = false;
|
|
1121
1297
|
let sampling = false;
|
|
1122
1298
|
let refused = false;
|
|
1299
|
+
let refreshed = false;
|
|
1123
1300
|
const run = async (startedAt) => {
|
|
1124
1301
|
const found = deps.leftovers.collect(startedAt);
|
|
1125
1302
|
const payload = foldOwnerless(
|
|
@@ -1183,6 +1360,9 @@ function createReporter(deps) {
|
|
|
1183
1360
|
last_report_outcome: outcome,
|
|
1184
1361
|
last_report_error: error
|
|
1185
1362
|
});
|
|
1363
|
+
const needs = sent.status === 200 ? needsContractOf(sent.text) : null;
|
|
1364
|
+
if (needs !== null && needs > CONTRACT_VERSION)
|
|
1365
|
+
refreshed = await deps.refresh(needs);
|
|
1186
1366
|
};
|
|
1187
1367
|
return {
|
|
1188
1368
|
async sample(at) {
|
|
@@ -1220,102 +1400,16 @@ function createReporter(deps) {
|
|
|
1220
1400
|
running = false;
|
|
1221
1401
|
});
|
|
1222
1402
|
},
|
|
1223
|
-
refused: () => refused
|
|
1224
|
-
|
|
1225
|
-
}
|
|
1226
|
-
|
|
1227
|
-
// src/machine-daemon/state.ts
|
|
1228
|
-
import {
|
|
1229
|
-
appendFileSync,
|
|
1230
|
-
chmodSync,
|
|
1231
|
-
existsSync as existsSync2,
|
|
1232
|
-
mkdirSync,
|
|
1233
|
-
readFileSync as readFileSync3,
|
|
1234
|
-
renameSync,
|
|
1235
|
-
statSync,
|
|
1236
|
-
writeFileSync
|
|
1237
|
-
} from "node:fs";
|
|
1238
|
-
import { homedir as homedir2 } from "node:os";
|
|
1239
|
-
import { dirname, join as join3 } from "node:path";
|
|
1240
|
-
var MACHINE_DAEMON_DIR_NAME = ".danxbot";
|
|
1241
|
-
function createStatusWriter(write) {
|
|
1242
|
-
let current = {
|
|
1243
|
-
pid: process.pid,
|
|
1244
|
-
last_beat_at: null,
|
|
1245
|
-
last_beat_ok: null,
|
|
1246
|
-
last_error: null,
|
|
1247
|
-
next_beat_at: null,
|
|
1248
|
-
last_report_at: null,
|
|
1249
|
-
last_report_outcome: null,
|
|
1250
|
-
last_report_error: null
|
|
1251
|
-
};
|
|
1252
|
-
const merge = (fields) => {
|
|
1253
|
-
current = { ...current, ...fields };
|
|
1254
|
-
write(current);
|
|
1255
|
-
};
|
|
1256
|
-
return { beat: merge, report: merge };
|
|
1257
|
-
}
|
|
1258
|
-
function danxbotHome() {
|
|
1259
|
-
return join3(homedir2(), MACHINE_DAEMON_DIR_NAME);
|
|
1260
|
-
}
|
|
1261
|
-
function daemonConfigPath(home = danxbotHome()) {
|
|
1262
|
-
return join3(home, "machine-daemon.json");
|
|
1263
|
-
}
|
|
1264
|
-
function daemonStatusPath(home = danxbotHome()) {
|
|
1265
|
-
return join3(home, "machine-daemon-status.json");
|
|
1266
|
-
}
|
|
1267
|
-
function daemonLogPath(home = danxbotHome()) {
|
|
1268
|
-
return join3(home, "machine-daemon.log");
|
|
1269
|
-
}
|
|
1270
|
-
function daemonBundleDir(home = danxbotHome()) {
|
|
1271
|
-
return join3(home, "machine-daemon");
|
|
1272
|
-
}
|
|
1273
|
-
function toolkitBundlePath(home = danxbotHome()) {
|
|
1274
|
-
return join3(daemonBundleDir(home), "machine-toolkit.js");
|
|
1275
|
-
}
|
|
1276
|
-
function writeOwnerOnlyJson(file, value) {
|
|
1277
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
1278
|
-
const tmp = `${file}.${process.pid}.tmp`;
|
|
1279
|
-
writeFileSync(tmp, `${JSON.stringify(value, null, 2)}
|
|
1280
|
-
`, {
|
|
1281
|
-
encoding: "utf8",
|
|
1282
|
-
mode: 384
|
|
1283
|
-
});
|
|
1284
|
-
if (process.platform !== "win32") chmodSync(tmp, 384);
|
|
1285
|
-
renameSync(tmp, file);
|
|
1286
|
-
}
|
|
1287
|
-
function readJsonIfPresent(file) {
|
|
1288
|
-
if (!existsSync2(file)) return null;
|
|
1289
|
-
return JSON.parse(readFileSync3(file, "utf8"));
|
|
1290
|
-
}
|
|
1291
|
-
var DAEMON_LOG_MAX_BYTES = 1024 * 1024;
|
|
1292
|
-
function createDaemonLog(file, now = () => /* @__PURE__ */ new Date(), maxBytes = DAEMON_LOG_MAX_BYTES) {
|
|
1293
|
-
let narrowed = false;
|
|
1294
|
-
return (line) => {
|
|
1295
|
-
try {
|
|
1296
|
-
mkdirSync(dirname(file), { recursive: true });
|
|
1297
|
-
if (!narrowed && existsSync2(file) && process.platform !== "win32")
|
|
1298
|
-
chmodSync(file, 384);
|
|
1299
|
-
narrowed = true;
|
|
1300
|
-
if (existsSync2(file) && statSync(file).size >= maxBytes)
|
|
1301
|
-
renameSync(file, `${file}.1`);
|
|
1302
|
-
appendFileSync(file, `${now().toISOString()} ${line}
|
|
1303
|
-
`, {
|
|
1304
|
-
encoding: "utf8",
|
|
1305
|
-
mode: 384
|
|
1306
|
-
});
|
|
1307
|
-
} catch (err) {
|
|
1308
|
-
console.error(
|
|
1309
|
-
`[machine-daemon] cannot write ${file}: ${err instanceof Error ? err.message : String(err)}`
|
|
1310
|
-
);
|
|
1311
|
-
}
|
|
1403
|
+
refused: () => refused,
|
|
1404
|
+
refreshed: () => refreshed
|
|
1312
1405
|
};
|
|
1313
1406
|
}
|
|
1314
1407
|
|
|
1315
1408
|
// src/machine-daemon/run.ts
|
|
1409
|
+
var REFRESHED_EXIT_CODE = 3;
|
|
1316
1410
|
function finishBeatStop(stop, disable) {
|
|
1317
1411
|
if (stop.reason === "refused") disable();
|
|
1318
|
-
return 0;
|
|
1412
|
+
return stop.reason === "refreshed" ? REFRESHED_EXIT_CODE : 0;
|
|
1319
1413
|
}
|
|
1320
1414
|
function readBearerOnDisk() {
|
|
1321
1415
|
try {
|
|
@@ -1364,6 +1458,7 @@ async function runDaemonProcess() {
|
|
|
1364
1458
|
toolkitPath: toolkitBundlePath()
|
|
1365
1459
|
}),
|
|
1366
1460
|
fetchFn,
|
|
1461
|
+
refresh: (needs) => refreshBundles({ needs, now: now(), spawnFn: spawn, log }),
|
|
1367
1462
|
leftovers: createSystemLeftoverTracker(log),
|
|
1368
1463
|
writeStatus: status.report,
|
|
1369
1464
|
log,
|
package/dist/machine-toolkit.js
CHANGED
|
@@ -612,7 +612,7 @@ var readOwnManifest = () => JSON.parse(
|
|
|
612
612
|
readFileSync(new URL("../../package.json", import.meta.url), "utf8")
|
|
613
613
|
).version;
|
|
614
614
|
var TOOLKIT_VERSION = resolveToolkitVersion(
|
|
615
|
-
false ? void 0 : "0.1.
|
|
615
|
+
false ? void 0 : "0.1.484",
|
|
616
616
|
readOwnManifest
|
|
617
617
|
);
|
|
618
618
|
function envelope(now) {
|
package/dist/result-limit.js
CHANGED
|
@@ -26,6 +26,9 @@ export function boundedResultText(result, deps = {}) {
|
|
|
26
26
|
return JSON.stringify({
|
|
27
27
|
ok: result.ok,
|
|
28
28
|
status: result.status,
|
|
29
|
+
...(result.app_version === undefined
|
|
30
|
+
? {}
|
|
31
|
+
: { app_version: result.app_version }),
|
|
29
32
|
reply_file: {
|
|
30
33
|
path: file,
|
|
31
34
|
bytes: Buffer.byteLength(body, "utf8"),
|
|
@@ -93,7 +93,7 @@ export const GET_ROUTE_BOARD_SCOPE = [
|
|
|
93
93
|
["/api/test-runs/quarantines", "refuse"],
|
|
94
94
|
["/api/test-runs/run-kind-stats", "refuse"],
|
|
95
95
|
["/api/test-runs/slowest", "refuse"],
|
|
96
|
-
["/api/test-runs/stale-
|
|
96
|
+
["/api/test-runs/stale-window", "refuse"],
|
|
97
97
|
["/api/test-runs/stats", "refuse"],
|
|
98
98
|
["/api/usage/series", "refuse"],
|
|
99
99
|
["/api/users", "refuse"],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@50arms/platform-mcp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.484",
|
|
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",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"!dist/machine-daemon/run.js",
|
|
14
14
|
"!dist/machine-daemon/entry.js",
|
|
15
15
|
"!dist/machine-daemon/report.js",
|
|
16
|
+
"!dist/machine-daemon/refresh.js",
|
|
16
17
|
"!dist/machine-toolkit/**",
|
|
17
18
|
"README.md"
|
|
18
19
|
],
|