mobile-automator 0.35.2 → 0.36.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -2
- package/src/cli.js +8 -4
- package/src/device/action-catalog.js +2 -2
- package/src/device/bridge.js +44 -16
- package/src/device/crash-model.js +23 -1
- package/src/device/device-model.js +27 -5
- package/src/device/engine-hints.js +17 -18
- package/src/device/failure-probe.js +8 -1
- package/src/device/process-identity.js +11 -5
- package/src/device/session-end.js +43 -13
- package/src/device/session-paths.js +14 -0
- package/src/observe/vocab.js +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mobile-automator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.36.1",
|
|
4
4
|
"description": "Host-agnostic mauto CLI for AI-driven mobile QA automation — analyzes a mobile project and drives devices through mobile-mcp.",
|
|
5
5
|
"homepage": "https://github.com/sh3lan93/mobile-automator#readme",
|
|
6
6
|
"repository": {
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"test:all": "jest"
|
|
35
35
|
},
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@mobilenext/mobile-mcp": "1.0.
|
|
37
|
+
"@mobilenext/mobile-mcp": "1.0.13",
|
|
38
38
|
"@modelcontextprotocol/sdk": "^1.29.0",
|
|
39
39
|
"ajv": "^8.20.0",
|
|
40
40
|
"commander": "^12.1.0"
|
package/src/cli.js
CHANGED
|
@@ -1411,14 +1411,18 @@ async function handleSessionStatus(
|
|
|
1411
1411
|
}
|
|
1412
1412
|
|
|
1413
1413
|
// endSession's failure outcomes (#156), each with the hint that is true for it.
|
|
1414
|
-
//
|
|
1415
|
-
// differs is WHY, and so what the user should do about it. One vague hint
|
|
1416
|
-
// to cover all
|
|
1414
|
+
// Every one of them may leave a daemon alive and holding the workspace lock;
|
|
1415
|
+
// what differs is WHY, and so what the user should do about it. One vague hint
|
|
1416
|
+
// used to cover them all.
|
|
1417
1417
|
const SESSION_END_FAILURE_HINTS = {
|
|
1418
1418
|
survived: (pid, pidFile) =>
|
|
1419
1419
|
`It is still alive after SIGTERM and SIGKILL and may still hold the workspace lock. Check it with \`ps -p ${pid}\` and stop it with \`kill -9 ${pid}\` (pidfile: ${pidFile}).`,
|
|
1420
1420
|
foreign: (pid, pidFile) =>
|
|
1421
|
-
`
|
|
1421
|
+
`Something answered the shutdown, but this pid cannot be tied to this workspace, so it was not signalled: its command line names another workspace, or names none and the workspace lock does not name it. Check it with \`ps -p ${pid}\` and stop it manually (pidfile: ${pidFile}).`,
|
|
1422
|
+
// #237: a daemon started by mauto <= 0.27.x carries no workspace marker, and
|
|
1423
|
+
// without an ack nothing else proves it ours.
|
|
1424
|
+
unproven: (pid, pidFile) =>
|
|
1425
|
+
`It did not answer the shutdown and its command line carries no workspace marker (a daemon started by mauto < 0.28), so this workspace cannot prove it is its own and it was not signalled. It may still hold the workspace lock. Check it with \`ps -p ${pid}\` and stop it with \`kill -9 ${pid}\` if it is this workspace's daemon (pidfile: ${pidFile}).`,
|
|
1422
1426
|
unverified: (pid, pidFile) =>
|
|
1423
1427
|
`It could not be identified (\`ps\` failed), so it was not signalled. Check it with \`ps -p ${pid}\` and stop it manually if it is this workspace's mauto daemon (pidfile: ${pidFile}).`,
|
|
1424
1428
|
};
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
//
|
|
25
25
|
// For 'verb' entries, `args` documents the mobile-mcp argument keys the bridge
|
|
26
26
|
// method must pass — deliberately explicit because the keys are inconsistent
|
|
27
|
-
// across the engine (launch=packageName, uninstall=
|
|
27
|
+
// across the engine (launch=packageName, uninstall=packageName, install=path).
|
|
28
28
|
//
|
|
29
29
|
// `stepFlags` records a scenario step field that changes how the verb runs:
|
|
30
30
|
// `{ field, value, flag }` means "when step[field] === value, pass flag". This
|
|
@@ -77,7 +77,7 @@ const ACTION_CATALOG = {
|
|
|
77
77
|
},
|
|
78
78
|
uninstall_app: {
|
|
79
79
|
resolution: 'verb', verb: 'uninstall', bridge: 'uninstallApp',
|
|
80
|
-
primitive: 'mobile_uninstall_app', args: ['
|
|
80
|
+
primitive: 'mobile_uninstall_app', args: ['packageName'],
|
|
81
81
|
},
|
|
82
82
|
open_url: {
|
|
83
83
|
resolution: 'verb', verb: 'open-url', bridge: 'openUrl',
|
package/src/device/bridge.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { normalize, parseElements } = require('./element-model');
|
|
4
|
-
const { normalizeDevices } = require('./device-model');
|
|
5
|
-
const { normalizeCrashes } = require('./crash-model');
|
|
4
|
+
const { deviceRecords, normalizeDevices, isIosSimulator } = require('./device-model');
|
|
5
|
+
const { normalizeCrashes, CrashListUnverifiableError } = require('./crash-model');
|
|
6
6
|
const { resolveSingleDevice } = require('./device-resolver');
|
|
7
7
|
const { centredSwipe } = require('./swipe-geometry');
|
|
8
8
|
const { isAppNotRunning } = require('./app-not-running');
|
|
@@ -94,18 +94,28 @@ class DeviceBridge {
|
|
|
94
94
|
return this._call('mobile_swipe_on_screen', args);
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
//
|
|
98
|
-
// one was provided, else the single auto-resolved device.
|
|
97
|
+
// The current device's record (id/name/platform/state/type): the pinned id
|
|
98
|
+
// when one was provided, else the single auto-resolved device. A pinned id
|
|
99
|
+
// that is not listed throws — callers treat that as "could not check".
|
|
100
|
+
async _currentDevice() {
|
|
101
|
+
const records = deviceRecords(await this._call('mobile_list_available_devices', {}));
|
|
102
|
+
const id = this._device || resolveSingleDevice(records); // throws DeviceResolutionError on 0/many
|
|
103
|
+
const match = records.find((d) => d.id === id);
|
|
104
|
+
if (!match) throw this._unknownPlatform(id);
|
|
105
|
+
return match;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_unknownPlatform(id) {
|
|
109
|
+
const err = new Error(`Cannot determine the platform of device "${id}".`);
|
|
110
|
+
err.hint = 'Ensure the device is listed by `mauto devices`.';
|
|
111
|
+
return err;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// Platform ('android'/'ios') of the current device.
|
|
99
115
|
async getPlatform() {
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
if (!match || !match.platform) {
|
|
104
|
-
const err = new Error(`Cannot determine the platform of device "${id}".`);
|
|
105
|
-
err.hint = 'Ensure the device is listed by `mauto devices`.';
|
|
106
|
-
throw err;
|
|
107
|
-
}
|
|
108
|
-
return String(match.platform).toLowerCase();
|
|
116
|
+
const device = await this._currentDevice();
|
|
117
|
+
if (!device.platform) throw this._unknownPlatform(device.id);
|
|
118
|
+
return String(device.platform).toLowerCase();
|
|
109
119
|
}
|
|
110
120
|
|
|
111
121
|
// Physical screen size in pixels, for geometry-based gestures. mobile-mcp
|
|
@@ -176,7 +186,7 @@ class DeviceBridge {
|
|
|
176
186
|
|
|
177
187
|
// Uninstall an app by its bundle/package identifier.
|
|
178
188
|
async uninstallApp(appId) {
|
|
179
|
-
return await this._call('mobile_uninstall_app', {
|
|
189
|
+
return await this._call('mobile_uninstall_app', { packageName: appId });
|
|
180
190
|
}
|
|
181
191
|
|
|
182
192
|
// Open a URL (deep link or web) on the device.
|
|
@@ -201,9 +211,27 @@ class DeviceBridge {
|
|
|
201
211
|
// come from the device log and AGE OUT; iOS device/simulator reports are files
|
|
202
212
|
// that PERSIST and accumulate across runs. Same call, under-inclusive on one
|
|
203
213
|
// platform and over-inclusive on the other — hence the `since` scoping in
|
|
204
|
-
// src/device/failure-probe.js.
|
|
214
|
+
// src/device/failure-probe.js.
|
|
215
|
+
//
|
|
216
|
+
// ONE platform exception, and only for an EMPTY list: on an iOS simulator the
|
|
217
|
+
// pinned engine (mobilecli 1.0.21, since mobilecli#482) returns `[]` on
|
|
218
|
+
// macOS 27 hosts even right after the app crashed — its filter requires an
|
|
219
|
+
// `is_simulated` key that those simulator reports lack (mobilecli#539). There
|
|
220
|
+
// an empty list is not evidence of "no crash", so we throw the typed
|
|
221
|
+
// CrashListUnverifiableError instead: callers treat it as "could not check"
|
|
222
|
+
// (handleCrashList fails the verb; the failure probe records it at info and
|
|
223
|
+
// leaves `crashes` absent), never as "found none". A
|
|
224
|
+
// non-empty list is real evidence on every platform and is returned as-is,
|
|
225
|
+
// and the device lookup is paid only on an empty list. Remove this guard
|
|
226
|
+
// when the pinned mobilecli fixes #539: re-capture
|
|
227
|
+
// tests/fixtures/device/mobile-mcp-<ver>/ios-sim.crashes.txt with a real
|
|
228
|
+
// crash in it, then drop the guard and its tests.
|
|
205
229
|
async listCrashes() {
|
|
206
|
-
|
|
230
|
+
const crashes = normalizeCrashes(await this._call('mobile_list_crashes', {}));
|
|
231
|
+
if (crashes.length === 0 && isIosSimulator(await this._currentDevice())) {
|
|
232
|
+
throw new CrashListUnverifiableError();
|
|
233
|
+
}
|
|
234
|
+
return crashes;
|
|
207
235
|
}
|
|
208
236
|
|
|
209
237
|
// The full text of one crash report. The engine returns a bare string (its
|
|
@@ -107,4 +107,26 @@ function crashTimestampMs(crash) {
|
|
|
107
107
|
return parsed > 0 ? parsed : null;
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
// The engine bug behind the one platform where an empty crash list is not
|
|
111
|
+
// evidence of "no crash": an iOS simulator on a macOS 27 host (mobilecli#539).
|
|
112
|
+
const MOBILECLI_ISSUE_539 = 'https://github.com/mobile-next/mobilecli/issues/539';
|
|
113
|
+
|
|
114
|
+
// Thrown by DeviceBridge#listCrashes when the engine's answer cannot be trusted.
|
|
115
|
+
// Typed (in the shape of device-resolver.js's DeviceResolutionError) so the
|
|
116
|
+
// failure probe can record it as the expected condition it is rather than as a
|
|
117
|
+
// broken engine, and so the `crash` verb maps it onto the `device` envelope.
|
|
118
|
+
class CrashListUnverifiableError extends Error {
|
|
119
|
+
constructor() {
|
|
120
|
+
super(
|
|
121
|
+
'The device engine cannot confirm the absence of crashes on an iOS simulator: it reports none even after a crash.'
|
|
122
|
+
);
|
|
123
|
+
this.name = 'CrashListUnverifiableError';
|
|
124
|
+
this.kind = 'device';
|
|
125
|
+
this.code = 'crash_list_unverifiable';
|
|
126
|
+
this.hint =
|
|
127
|
+
`On an iOS simulator an empty crash list is not evidence that the app did not crash (upstream bug ${MOBILECLI_ISSUE_539}). ` +
|
|
128
|
+
'Treat the crash status as unknown, and check that the app is still running by other means (e.g. `mauto elements` or a screenshot).';
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
module.exports = { normalizeCrashes, crashTimestampMs, CrashListUnverifiableError };
|
|
@@ -5,12 +5,16 @@
|
|
|
5
5
|
// — and drop every other raw mobile-mcp field (no resource-id, no OS-specific
|
|
6
6
|
// internals). `os` is mapped onto `platform` so the output reads the same way
|
|
7
7
|
// across the Android/iOS/simulator variants mobile-mcp returns.
|
|
8
|
+
//
|
|
9
|
+
// deviceRecords is the ONE parser of the raw list. Its records also carry
|
|
10
|
+
// `type` (simulator/emulator/real), which DeviceBridge needs internally;
|
|
11
|
+
// normalizeDevices is only the public 4-key view of those records.
|
|
8
12
|
|
|
9
13
|
function nullable(v) {
|
|
10
14
|
return v === undefined || v === null || v === '' ? null : v;
|
|
11
15
|
}
|
|
12
16
|
|
|
13
|
-
function
|
|
17
|
+
function recordOf(raw) {
|
|
14
18
|
if (!raw || typeof raw !== 'object') return null;
|
|
15
19
|
|
|
16
20
|
const id = nullable(
|
|
@@ -35,10 +39,12 @@ function normalizeOne(raw) {
|
|
|
35
39
|
|
|
36
40
|
const state = nullable(raw.state !== undefined ? raw.state : raw.status);
|
|
37
41
|
|
|
38
|
-
|
|
42
|
+
const type = nullable(raw.type);
|
|
43
|
+
|
|
44
|
+
return { id: String(id), name, platform, state, type };
|
|
39
45
|
}
|
|
40
46
|
|
|
41
|
-
function
|
|
47
|
+
function deviceRecords(raw) {
|
|
42
48
|
let list;
|
|
43
49
|
if (Array.isArray(raw)) {
|
|
44
50
|
list = raw;
|
|
@@ -50,10 +56,26 @@ function normalizeDevices(raw) {
|
|
|
50
56
|
|
|
51
57
|
const out = [];
|
|
52
58
|
for (const item of list) {
|
|
53
|
-
const d =
|
|
59
|
+
const d = recordOf(item);
|
|
54
60
|
if (d) out.push(d);
|
|
55
61
|
}
|
|
56
62
|
return out;
|
|
57
63
|
}
|
|
58
64
|
|
|
59
|
-
|
|
65
|
+
function publicView({ id, name, platform, state }) {
|
|
66
|
+
return { id, name, platform, state };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function normalizeDevices(raw) {
|
|
70
|
+
return deviceRecords(raw).map(publicView);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function isIosSimulator(record) {
|
|
74
|
+
if (!record) return false;
|
|
75
|
+
return (
|
|
76
|
+
String(record.platform || '').toLowerCase() === 'ios' &&
|
|
77
|
+
String(record.type || '').toLowerCase() === 'simulator'
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = { deviceRecords, normalizeDevices, isIosSimulator };
|
|
@@ -7,14 +7,14 @@ const path = require('path');
|
|
|
7
7
|
// tell the user what to do. Pure: message in, hint string (or null) out. It
|
|
8
8
|
// never throws — a hint classifier that crashes would turn a device failure
|
|
9
9
|
// into an internal one. Found by the #199 real-device run against mobile-mcp
|
|
10
|
-
// 1.0.5 / mobilecli 1.0.13
|
|
10
|
+
// 1.0.5 / mobilecli 1.0.13; the missing-agent case was re-captured against
|
|
11
|
+
// mobile-mcp 1.0.13 (#248), which installs the agent itself, so that hint now
|
|
12
|
+
// covers only a failed automatic install.
|
|
11
13
|
//
|
|
12
14
|
// mauto only TELLS the user which command to run; it never runs mobilecli
|
|
13
15
|
// itself (the device is driven only through mauto verbs, which wrap
|
|
14
16
|
// mobile-mcp).
|
|
15
17
|
|
|
16
|
-
const ISSUE_459 = 'https://github.com/mobile-next/mobile-mcp/issues/459';
|
|
17
|
-
|
|
18
18
|
// The mobilecli that the pinned mobile-mcp actually spawns — resolved from
|
|
19
19
|
// mobile-mcp's own directory, since that is the copy whose agent the device
|
|
20
20
|
// must match. Null when it cannot be resolved; the caller omits `@<version>`.
|
|
@@ -35,21 +35,21 @@ function safeVersion(resolver) {
|
|
|
35
35
|
}
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
// B1 (iOS):
|
|
39
|
-
//
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
|
|
38
|
+
// B1 (iOS): the simulator is missing the DeviceKit agent and the engine's
|
|
39
|
+
// automatic install of it failed (e.g. offline, or `simctl install` failing).
|
|
40
|
+
// The trigger is the shape captured against mobile-mcp 1.0.13 (#248,
|
|
41
|
+
// tests/fixtures/device/mobile-mcp-1.0.13/ios-sim.agent-install-failed.txt):
|
|
42
|
+
// execFileSync's `Command failed: <mobilecli> agent install --device <udid>`.
|
|
43
|
+
// The 1.0.5-era "Agent is not installed on the device" (#199) is unreachable
|
|
44
|
+
// from the pinned engine, which catches `agent status`'s exit itself.
|
|
45
|
+
const FAILED_AGENT_INSTALL = /Command failed:[^\n]*\bagent install --device\s+(\S+)/;
|
|
46
|
+
|
|
47
|
+
function missingAgentHint(udid, resolver) {
|
|
43
48
|
const version = safeVersion(resolver);
|
|
44
49
|
const pkg = version ? `mobilecli@${version}` : 'mobilecli';
|
|
45
|
-
const cmd = `npx -y ${pkg} agent install --device`;
|
|
46
|
-
const m = /--device\s+(\S+)/.exec(message);
|
|
47
|
-
const install = m
|
|
48
|
-
? `\`${cmd} ${m[1]}\``
|
|
49
|
-
: `\`${cmd}\` followed by the simulator's id from \`mauto devices\``;
|
|
50
50
|
return (
|
|
51
|
-
'The simulator is missing the device agent the engine needs
|
|
52
|
-
`
|
|
51
|
+
'The simulator is missing the device agent the engine needs. The engine normally installs it ' +
|
|
52
|
+
`automatically, but that install failed. Install it once by hand with \`npx -y ${pkg} agent install --device ${udid}\` ` +
|
|
53
53
|
'(needs network, takes a few seconds), then retry.'
|
|
54
54
|
);
|
|
55
55
|
}
|
|
@@ -80,9 +80,8 @@ const OUTSIDE_ALLOWED_DIRS_HINT =
|
|
|
80
80
|
function engineHint(message, { mobilecliVersion = resolveMobilecliVersion } = {}) {
|
|
81
81
|
try {
|
|
82
82
|
if (typeof message !== 'string' || !message) return null;
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
83
|
+
const agentInstall = FAILED_AGENT_INSTALL.exec(message);
|
|
84
|
+
if (agentInstall) return missingAgentHint(agentInstall[1], mobilecliVersion);
|
|
86
85
|
const notFound = /Device "([^"]+)" not found/.exec(message);
|
|
87
86
|
if (notFound) return deviceNotFoundHint(notFound[1]);
|
|
88
87
|
if (/not in the list of allowed directories/.test(message)) return OUTSIDE_ALLOWED_DIRS_HINT;
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
// the genuinely undetermined case (skipped, failed, timed out, unscoped)
|
|
60
60
|
// leaves the envelope untouched.
|
|
61
61
|
|
|
62
|
-
const { crashTimestampMs } = require('./crash-model');
|
|
62
|
+
const { crashTimestampMs, CrashListUnverifiableError } = require('./crash-model');
|
|
63
63
|
const { record } = require('../observe/recorder');
|
|
64
64
|
|
|
65
65
|
// The probe's own budget, deliberately far below the daemon's per-call timeout
|
|
@@ -189,6 +189,13 @@ async function probeCrashes({
|
|
|
189
189
|
timeoutMs
|
|
190
190
|
);
|
|
191
191
|
} catch (err) {
|
|
192
|
+
// An iOS simulator's untrustworthy empty list (mobilecli#539) is an
|
|
193
|
+
// expected engine limitation, not a broken probe: info, not a warn on
|
|
194
|
+
// the user's stderr. Either way `crashes` stays absent ("unknown").
|
|
195
|
+
if (err instanceof CrashListUnverifiableError) {
|
|
196
|
+
record({ level: 'info', src: 'cli', event: 'crash.probe_unverifiable', verb }, recordOpts);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
192
199
|
record(
|
|
193
200
|
{
|
|
194
201
|
level: 'warn',
|
|
@@ -66,10 +66,15 @@ function isZombie(
|
|
|
66
66
|
// 'dead' - no such process (ESRCH or any non-EPERM kill error)
|
|
67
67
|
// 'ours' - alive AND its command line contains `mauto-session-daemon`
|
|
68
68
|
// (and, when `projectRoot` is given, names THAT workspace)
|
|
69
|
-
// 'foreign' - only with `projectRoot`: alive and a mauto daemon
|
|
70
|
-
// command line
|
|
71
|
-
// workspace's
|
|
72
|
-
//
|
|
69
|
+
// 'foreign' - only with `projectRoot`: alive and a mauto daemon whose
|
|
70
|
+
// command line carries a workspace marker naming ANOTHER
|
|
71
|
+
// workspace (#156) — or this one's under a salt we cannot read.
|
|
72
|
+
// Never ours to signal.
|
|
73
|
+
// 'unmarked'- only with `projectRoot`: alive and a mauto daemon whose command
|
|
74
|
+
// line carries no workspace marker at all (#237) — one started by
|
|
75
|
+
// mauto <= 0.27.x, or an editor open on the bin. Its line cannot
|
|
76
|
+
// prove whose it is either way; `session end` decides from the
|
|
77
|
+
// socket ack and the lock (src/device/session-end.js).
|
|
73
78
|
// 'other' - alive AND its command line does NOT (recycled pid)
|
|
74
79
|
// 'unknown' - alive but `ps` itself failed (e.g. no `ps` on the host) —
|
|
75
80
|
// treated as 'ours' by cleanStale: never clean when unsure.
|
|
@@ -112,7 +117,8 @@ function pidIdentity(
|
|
|
112
117
|
);
|
|
113
118
|
if (!/mauto-session-daemon/.test(out)) return 'other';
|
|
114
119
|
if (projectRoot == null) return 'ours';
|
|
115
|
-
|
|
120
|
+
if (paths.commandNamesWorkspace(out, projectRoot, readSalt(projectRoot))) return 'ours';
|
|
121
|
+
return paths.commandHasWorkspaceMarker(out) ? 'foreign' : 'unmarked';
|
|
116
122
|
} catch (_) {
|
|
117
123
|
return 'unknown';
|
|
118
124
|
}
|
|
@@ -31,11 +31,17 @@ const END_BUDGET_MS = sessionDaemon.DAEMON_CALL_TIMEOUT_MS + 10000;
|
|
|
31
31
|
// 'already_stopped' - nothing acked and nothing of ours was running
|
|
32
32
|
// 'forced' - ours, stuck in teardown, terminated by a group kill
|
|
33
33
|
// 'survived' - ours, stuck, and still alive after SIGTERM + SIGKILL
|
|
34
|
-
// 'foreign' - acked, but the live pid
|
|
35
|
-
//
|
|
34
|
+
// 'foreign' - acked, but the live pid cannot be tied to this
|
|
35
|
+
// workspace: its command line names another one, or it
|
|
36
|
+
// names none and the lock does not name the same pid —
|
|
37
|
+
// reported, never signalled
|
|
38
|
+
// 'unproven' - not acked, and the live pid is a mauto daemon whose
|
|
39
|
+
// command line names no workspace (mauto <= 0.27.x, #237):
|
|
40
|
+
// nothing proves it ours, so it was never signalled, and
|
|
41
|
+
// it may still hold this workspace's lock
|
|
36
42
|
// 'unverified' - the pid could not be identified (`ps` failed), so it
|
|
37
43
|
// was never signalled
|
|
38
|
-
// The last
|
|
44
|
+
// The last four are failures: a daemon may still be alive.
|
|
39
45
|
async function endSession(
|
|
40
46
|
projectRoot,
|
|
41
47
|
{
|
|
@@ -45,12 +51,16 @@ async function endSession(
|
|
|
45
51
|
// workspace's daemon through a copied or recycled pidfile.
|
|
46
52
|
identify = (pid) => processIdentity.pidIdentity(pid, { projectRoot }),
|
|
47
53
|
readPid = () => processIdentity.readPidFile(paths.pidFilePath(projectRoot)),
|
|
54
|
+
// The lock names the daemon that holds this workspace (#237). It is the
|
|
55
|
+
// second witness for a daemon whose command line carries no marker.
|
|
56
|
+
readLockPid = () => processIdentity.readPidFile(paths.lockPath(projectRoot)),
|
|
48
57
|
budgetMs = END_BUDGET_MS,
|
|
49
58
|
} = {}
|
|
50
59
|
) {
|
|
51
|
-
// Read BEFORE asking: a daemon that exits cleanly removes its pidfile
|
|
52
|
-
// need to know whom to watch.
|
|
60
|
+
// Read BEFORE asking: a daemon that exits cleanly removes its pidfile and its
|
|
61
|
+
// lock, and we need to know whom to watch — and who held the lock.
|
|
53
62
|
const pid = readPid();
|
|
63
|
+
const lockPid = readLockPid();
|
|
54
64
|
const acked = await client.requestShutdown(projectRoot);
|
|
55
65
|
const report = (outcome) => ({ outcome, pid: pid || null });
|
|
56
66
|
// The daemon is gone: it stopped because we asked, or was never running.
|
|
@@ -69,13 +79,32 @@ async function endSession(
|
|
|
69
79
|
case 'other': // the pid was recycled: our daemon is gone
|
|
70
80
|
return gone();
|
|
71
81
|
case 'foreign':
|
|
72
|
-
// A mauto daemon whose command line
|
|
73
|
-
//
|
|
74
|
-
// Never signalled. Without an ack nothing answered on
|
|
75
|
-
// of ours was running. With an ack
|
|
76
|
-
//
|
|
77
|
-
//
|
|
82
|
+
// A mauto daemon whose command line names ANOTHER workspace (or this
|
|
83
|
+
// one's under a salt we cannot read): reached through a copied or
|
|
84
|
+
// recycled pidfile. Never signalled. Without an ack nothing answered on
|
|
85
|
+
// OUR socket, so none of ours was running. With an ack something of ours
|
|
86
|
+
// answered, but this pid is provably not it, so it is reported rather
|
|
87
|
+
// than killed. (Marker-less daemons arrive as 'unmarked' below, #237.)
|
|
78
88
|
return report(acked ? 'foreign' : 'already_stopped');
|
|
89
|
+
case 'unmarked':
|
|
90
|
+
// A mauto daemon whose command line names no workspace at all: one
|
|
91
|
+
// started by mauto <= 0.27.x (#237). Its line proves nothing, so two
|
|
92
|
+
// other witnesses must agree before it is signalled:
|
|
93
|
+
// - the ack: the socket lives inside this workspace's .session/ and a
|
|
94
|
+
// Unix socket only accepts on the inode the daemon bound, so a
|
|
95
|
+
// `cp -R`'d workspace can never make the original's daemon answer;
|
|
96
|
+
// - the lock naming this same pid, read before the ack, so the daemon
|
|
97
|
+
// that answered is the one we are about to kill.
|
|
98
|
+
// 0.27.x spawned it detached, so it leads its own process group and the
|
|
99
|
+
// group kill reaches its engine too.
|
|
100
|
+
if (acked && lockPid === pid) {
|
|
101
|
+
return report((await reaper.terminate(pid, { group: true })) === 'dead' ? 'forced' : 'survived');
|
|
102
|
+
}
|
|
103
|
+
// Acked, but the lock does not tie the answer to this pid: report it.
|
|
104
|
+
// Not acked: nothing proves it ours — but nor is it proven gone, and
|
|
105
|
+
// cleanStale (which asks without a root) still respects its lock, so
|
|
106
|
+
// 'already_stopped' would be a lie the next spawn trips over.
|
|
107
|
+
return report(acked ? 'foreign' : 'unproven');
|
|
79
108
|
case 'ours':
|
|
80
109
|
return report((await reaper.terminate(pid, { group: true })) === 'dead' ? 'forced' : 'survived');
|
|
81
110
|
default: // 'unknown': never signal a process we cannot identify
|
|
@@ -84,8 +113,9 @@ async function endSession(
|
|
|
84
113
|
}
|
|
85
114
|
|
|
86
115
|
// Outcomes after which no daemon of ours is left holding the workspace.
|
|
87
|
-
// Anything else ('survived', 'foreign', 'unverified') may leave
|
|
88
|
-
// daemon alive — spawning then would just find it answering pings
|
|
116
|
+
// Anything else ('survived', 'foreign', 'unproven', 'unverified') may leave
|
|
117
|
+
// the stale daemon alive — spawning then would just find it answering pings,
|
|
118
|
+
// or (for 'unproven') find its lock still held. Lives beside
|
|
89
119
|
// the vocabulary it classifies so a new outcome can never drift from its
|
|
90
120
|
// classification (#251's restart path, resolve-connection.js, reads it).
|
|
91
121
|
const ENDED_OUTCOMES = new Set(['stopped', 'forced', 'already_stopped']);
|
|
@@ -137,6 +137,19 @@ function commandNamesWorkspace(command, projectRoot, salt = null) {
|
|
|
137
137
|
return line.endsWith(` ${LEGACY_DAEMON_ROOT_FLAG} ${path.resolve(projectRoot)}`);
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
// Does a `ps -o command=` line carry a workspace marker of EITHER kind, whatever
|
|
141
|
+
// workspace it names (#237)? Daemons started by mauto <= 0.27.x carry none, and
|
|
142
|
+
// "names no workspace" is a different fact from "names another workspace": the
|
|
143
|
+
// first cannot be told apart by its command line at all, so `session end`
|
|
144
|
+
// falls back to the socket and the lock; the second is proof it is not ours.
|
|
145
|
+
// Matched as a whole argv token (whitespace or line edge on both sides), so a
|
|
146
|
+
// path that merely contains the flag's text is not a marker.
|
|
147
|
+
const MARKER_FLAG_TOKEN = new RegExp(`(?:^|\\s)(?:${DAEMON_WORKSPACE_FLAG}|${LEGACY_DAEMON_ROOT_FLAG})(?=\\s|$)`);
|
|
148
|
+
|
|
149
|
+
function commandHasWorkspaceMarker(command) {
|
|
150
|
+
return MARKER_FLAG_TOKEN.test(String(command || ''));
|
|
151
|
+
}
|
|
152
|
+
|
|
140
153
|
// A path as it should appear in a child's argv (or an env var the child execs
|
|
141
154
|
// by) (#236). `ps` shows every process's argv to every user on the host, and
|
|
142
155
|
// when mobile-automator is installed inside the project (a devDependency, a
|
|
@@ -191,5 +204,6 @@ module.exports = {
|
|
|
191
204
|
LEGACY_DAEMON_ROOT_FLAG,
|
|
192
205
|
daemonArgv,
|
|
193
206
|
commandNamesWorkspace,
|
|
207
|
+
commandHasWorkspaceMarker,
|
|
194
208
|
argvPath,
|
|
195
209
|
};
|