@xkwdstore/agent 2.0.6 → 2.1.0
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/README.md +37 -11
- package/package.json +11 -2
- package/scripts/install-machine-agent.ps1 +351 -0
- package/scripts/worker-entry.js +53 -0
- package/src/actions/comment.js +153 -154
- package/src/actions/confirmation.js +129 -0
- package/src/actions/follow.js +179 -123
- package/src/actions/tweet.js +107 -107
- package/src/brokerWorkerBridge.js +352 -0
- package/src/browserIsolation.js +691 -0
- package/src/cli.js +490 -123
- package/src/deviceKeyStore.js +446 -71
- package/src/legacyStartup.js +195 -0
- package/src/machineLock.js +202 -0
- package/src/machineState.js +354 -0
- package/src/processLock.js +15 -2
- package/src/proxyEgress.js +28 -4
- package/src/sessionHandler.js +560 -33
- package/src/sessionRegistry.js +280 -0
- package/src/worker.js +253 -0
package/src/actions/comment.js
CHANGED
|
@@ -1,154 +1,153 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Comment action — post a reply/comment on a tweet.
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
'
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
module.exports = { comment };
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Comment action — post a reply/comment on a tweet.
|
|
5
|
+
*
|
|
6
|
+
* NON-IDEMPOTENT. The submit mutation is dispatched AT MOST ONCE. After that
|
|
7
|
+
* the outcome is established by PASSIVE, read-only confirmation bound to the
|
|
8
|
+
* exact compose surface used; if that cannot prove success the result is
|
|
9
|
+
* UNKNOWN_OUTCOME (never a second submit, never a false success).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { sleep, humanDelay, randDelay, naturalNavigate, humanPreAction, humanType, humanClick, detectStage } = require('./helpers');
|
|
13
|
+
const { captureComposeSurface, countArticlesWithText, confirmSubmission, SUBMIT_CONFIRM_TIMEOUT_MS } = require('./confirmation');
|
|
14
|
+
|
|
15
|
+
const FAILURE_TOAST = /something went wrong|try again|failed/i;
|
|
16
|
+
|
|
17
|
+
async function comment(page, payload) {
|
|
18
|
+
const { tweetUrl, commentText } = payload;
|
|
19
|
+
if (!tweetUrl || !commentText) throw new Error('tweetUrl and commentText required');
|
|
20
|
+
|
|
21
|
+
// Navigate to the tweet
|
|
22
|
+
await page.goto(tweetUrl, { waitUntil: 'domcontentloaded', timeout: 30000 }).catch(() => {});
|
|
23
|
+
await randDelay(1800, 3200);
|
|
24
|
+
|
|
25
|
+
if (page.url().includes('/login') || page.url().includes('/i/flow')) {
|
|
26
|
+
throw new Error('Not logged in — open browser and login first');
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Find and click the reply button
|
|
30
|
+
const tweetIdMatch = tweetUrl.match(/\/status\/(\d+)/);
|
|
31
|
+
const tweetId = tweetIdMatch ? tweetIdMatch[1] : null;
|
|
32
|
+
const replySelectors = [
|
|
33
|
+
'button[data-testid="reply"]',
|
|
34
|
+
'[data-testid="reply"]',
|
|
35
|
+
'button[aria-label*="Reply"]',
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
let replyClicked = false;
|
|
39
|
+
for (const sel of replySelectors) {
|
|
40
|
+
const scoped = tweetId ? `article:has(a[href*="/status/${tweetId}"]) ${sel}` : sel;
|
|
41
|
+
const btn = page.locator(scoped).first();
|
|
42
|
+
if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
|
|
43
|
+
await humanClick(page, btn);
|
|
44
|
+
replyClicked = true;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!replyClicked && tweetId) {
|
|
50
|
+
// Fallback for tweet detail pages where the target article does not include
|
|
51
|
+
// a status link back to itself.
|
|
52
|
+
for (const sel of replySelectors) {
|
|
53
|
+
const btn = page.locator(sel).first();
|
|
54
|
+
if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
|
|
55
|
+
await humanClick(page, btn);
|
|
56
|
+
replyClicked = true;
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!replyClicked) throw new Error('Could not find reply button');
|
|
63
|
+
|
|
64
|
+
// Wait for dialog or inline editor
|
|
65
|
+
await sleep(1500);
|
|
66
|
+
const editorSelectors = [
|
|
67
|
+
'div[role="dialog"] [data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
|
|
68
|
+
'[data-testid="tweetTextarea_0"] div[contenteditable="true"][role="textbox"]',
|
|
69
|
+
'.public-DraftEditor-content',
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
let editor = null;
|
|
73
|
+
for (const sel of editorSelectors) {
|
|
74
|
+
const el = page.locator(sel).first();
|
|
75
|
+
if (await el.isVisible({ timeout: 5000 }).catch(() => false)) {
|
|
76
|
+
editor = el;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!editor) throw new Error('Could not find comment editor');
|
|
82
|
+
await editor.click();
|
|
83
|
+
await humanDelay(300, 600);
|
|
84
|
+
|
|
85
|
+
// Clear any existing text
|
|
86
|
+
await page.keyboard.press('Control+a');
|
|
87
|
+
await page.keyboard.press('Backspace');
|
|
88
|
+
|
|
89
|
+
// Type comment
|
|
90
|
+
const isRTL = /[\u0600-\u06FF\u0750-\u077F]/.test(commentText);
|
|
91
|
+
if (isRTL) {
|
|
92
|
+
await page.evaluate(() => {
|
|
93
|
+
const el = document.querySelector('[data-testid="tweetTextarea_0"]');
|
|
94
|
+
if (el) el.setAttribute('dir', 'rtl');
|
|
95
|
+
});
|
|
96
|
+
const words = commentText.split(' ');
|
|
97
|
+
for (const word of words) {
|
|
98
|
+
await page.evaluate((w) => document.execCommand('insertText', false, w + ' '), word);
|
|
99
|
+
await humanDelay(50, 100);
|
|
100
|
+
}
|
|
101
|
+
} else {
|
|
102
|
+
await humanType(page, commentText, { delayMin: 50, delayMax: 150 });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
await humanDelay(500, 1000);
|
|
106
|
+
|
|
107
|
+
// ── Bind confirmation to the EXACT compose surface used ──────────────────
|
|
108
|
+
const surface = await captureComposeSurface(page);
|
|
109
|
+
const matchesBefore = (await countArticlesWithText(page, commentText)) || 0;
|
|
110
|
+
|
|
111
|
+
// ── ONE submission. Exactly one of {send-button click, Ctrl+Enter}. ──────
|
|
112
|
+
const sendSelectors = [
|
|
113
|
+
'div[role="dialog"] [data-testid="tweetButton"]',
|
|
114
|
+
'[data-testid="tweetButton"]',
|
|
115
|
+
'[data-testid="tweetButtonInline"]',
|
|
116
|
+
];
|
|
117
|
+
|
|
118
|
+
let submitted = false;
|
|
119
|
+
for (const sel of sendSelectors) {
|
|
120
|
+
const btn = page.locator(sel).first();
|
|
121
|
+
if (await btn.isVisible({ timeout: 3000 }).catch(() => false)) {
|
|
122
|
+
await humanClick(page, btn);
|
|
123
|
+
submitted = true;
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (!submitted) {
|
|
128
|
+
await page.keyboard.press('Control+Enter');
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ── PASSIVE confirmation. NEVER submit again. ────────────────────────────
|
|
132
|
+
const confirmed = await confirmSubmission(page, {
|
|
133
|
+
surface, text: commentText, matchesBefore, timeoutMs: SUBMIT_CONFIRM_TIMEOUT_MS,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (confirmed) return { success: true, message: 'Comment posted' };
|
|
137
|
+
|
|
138
|
+
// Not confirmed: a clear X failure toast is a KNOWN failure (the submit is
|
|
139
|
+
// never repeated either way).
|
|
140
|
+
const bodyText = await page.evaluate(() => document.body.innerText).catch(() => '');
|
|
141
|
+
if (FAILURE_TOAST.test(bodyText)) {
|
|
142
|
+
throw new Error('Comment submit reported a failure');
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
success: false,
|
|
147
|
+
code: 'UNKNOWN_OUTCOME',
|
|
148
|
+
outcome: 'unknown',
|
|
149
|
+
error: 'Comment submit could not be confirmed; outcome unknown',
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = { comment };
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Passive submission confirmation for NON-IDEMPOTENT actions (tweet, comment).
|
|
5
|
+
*
|
|
6
|
+
* Invariant: once ANY submit mutation has been dispatched, this module performs
|
|
7
|
+
* NO further mutation. It only OBSERVES. If positive evidence cannot be
|
|
8
|
+
* established it reports "unconfirmed" and the caller returns UNKNOWN_OUTCOME.
|
|
9
|
+
*
|
|
10
|
+
* The false-negative this fixes: the old code waited on the GLOBAL selector
|
|
11
|
+
* `[data-testid="tweetTextarea_0"]` to detach, then pressed Control+Enter a
|
|
12
|
+
* SECOND time. On a tweet page the reply MODAL closes but an INLINE
|
|
13
|
+
* "Post your reply" editor remains, so the global selector never detaches, the
|
|
14
|
+
* second Ctrl+Enter retried a non-idempotent post, and the result was still
|
|
15
|
+
* reported UNKNOWN despite a successful submission. Confirmation must be bound
|
|
16
|
+
* to the EXACT compose surface that was submitted.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const { sleep } = require('./helpers');
|
|
20
|
+
|
|
21
|
+
/** Parse a bounded integer env override; invalid/out-of-range falls back. */
|
|
22
|
+
function boundedIntEnv(name, def, min, max) {
|
|
23
|
+
const raw = process.env[name];
|
|
24
|
+
if (raw === undefined || raw === null || raw === '') return def;
|
|
25
|
+
const v = Number.parseInt(String(raw), 10);
|
|
26
|
+
if (!Number.isFinite(v) || v < min || v > max) return def;
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Bounded and comfortably below the server runAction budget (180000 ms).
|
|
31
|
+
const SUBMIT_CONFIRM_TIMEOUT_MS = boundedIntEnv('LOCAL_AGENT_SUBMIT_CONFIRM_TIMEOUT_MS', 15000, 2000, 60000);
|
|
32
|
+
const CONFIRM_POLL_MS = boundedIntEnv('LOCAL_AGENT_SUBMIT_CONFIRM_POLL_MS', 250, 50, 2000);
|
|
33
|
+
|
|
34
|
+
const DEFAULT_EDITOR_SELECTOR = '[data-testid="tweetTextarea_0"]';
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Capture the EXACT compose surface in use, IMMEDIATELY before submission.
|
|
38
|
+
* Read-only. Returns a descriptor the confirmation binds to.
|
|
39
|
+
*/
|
|
40
|
+
async function captureComposeSurface(page, editorSelector = DEFAULT_EDITOR_SELECTOR) {
|
|
41
|
+
let dialogCount = 0;
|
|
42
|
+
try { dialogCount = await page.locator('div[role="dialog"]').count(); } catch { dialogCount = 0; }
|
|
43
|
+
const inDialog = dialogCount > 0;
|
|
44
|
+
const scope = inDialog ? `div[role="dialog"] ${editorSelector}` : editorSelector;
|
|
45
|
+
let scopeCount = 0;
|
|
46
|
+
try { scopeCount = await page.locator(scope).count(); } catch { scopeCount = 0; }
|
|
47
|
+
return { editorSelector, inDialog, scope, scopeCountBefore: scopeCount };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Read-only: has the captured compose surface closed / detached? */
|
|
51
|
+
async function composeSurfaceGone(page, surface) {
|
|
52
|
+
if (!surface) return false;
|
|
53
|
+
try {
|
|
54
|
+
if (surface.inDialog) {
|
|
55
|
+
const dialogs = await page.locator('div[role="dialog"]').count();
|
|
56
|
+
if (dialogs === 0) return true;
|
|
57
|
+
}
|
|
58
|
+
const n = await page.locator(surface.scope).count();
|
|
59
|
+
return n === 0;
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Read-only: how many `article` elements contain the exact submitted text?
|
|
67
|
+
* Used as STRONG secondary evidence of a new reply/post (bound to the exact
|
|
68
|
+
* text; the caller compares against the pre-submit count so a pre-existing
|
|
69
|
+
* identical item never counts).
|
|
70
|
+
*/
|
|
71
|
+
async function countArticlesWithText(page, text) {
|
|
72
|
+
if (!text) return null;
|
|
73
|
+
try {
|
|
74
|
+
return await page.evaluate((needle) => {
|
|
75
|
+
const arts = Array.from(document.querySelectorAll('article'));
|
|
76
|
+
return arts.filter((a) => (a.innerText || '').includes(needle)).length;
|
|
77
|
+
}, text);
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* PASSIVE confirmation loop. NEVER clicks, types, or presses. Bounded.
|
|
85
|
+
* Returns true only on positive evidence bound to the captured surface:
|
|
86
|
+
* - the exact compose surface used has closed/detached, OR
|
|
87
|
+
* - a NEW article containing the exact submitted text appeared, OR
|
|
88
|
+
* - an action-supplied extraProbe() (read-only) reports a known post-submit state.
|
|
89
|
+
*/
|
|
90
|
+
async function confirmSubmission(page, opts = {}) {
|
|
91
|
+
const {
|
|
92
|
+
surface = null,
|
|
93
|
+
text = null,
|
|
94
|
+
matchesBefore = 0,
|
|
95
|
+
timeoutMs = SUBMIT_CONFIRM_TIMEOUT_MS,
|
|
96
|
+
pollMs = CONFIRM_POLL_MS,
|
|
97
|
+
extraProbe = null,
|
|
98
|
+
sleepFn = sleep,
|
|
99
|
+
now = Date.now,
|
|
100
|
+
} = opts;
|
|
101
|
+
|
|
102
|
+
const probe = async () => {
|
|
103
|
+
if (await composeSurfaceGone(page, surface)) return true;
|
|
104
|
+
if (text) {
|
|
105
|
+
const matches = await countArticlesWithText(page, text);
|
|
106
|
+
if (matches !== null && matches > matchesBefore) return true;
|
|
107
|
+
}
|
|
108
|
+
if (extraProbe) { try { if (await extraProbe()) return true; } catch { /* read-only probe must never throw */ } }
|
|
109
|
+
return false;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const deadline = now() + timeoutMs;
|
|
113
|
+
while (now() < deadline) {
|
|
114
|
+
if (await probe()) return true;
|
|
115
|
+
await sleepFn(pollMs);
|
|
116
|
+
}
|
|
117
|
+
return await probe();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
module.exports = {
|
|
121
|
+
captureComposeSurface,
|
|
122
|
+
composeSurfaceGone,
|
|
123
|
+
countArticlesWithText,
|
|
124
|
+
confirmSubmission,
|
|
125
|
+
boundedIntEnv,
|
|
126
|
+
SUBMIT_CONFIRM_TIMEOUT_MS,
|
|
127
|
+
CONFIRM_POLL_MS,
|
|
128
|
+
DEFAULT_EDITOR_SELECTOR,
|
|
129
|
+
};
|