screenhand 0.1.1 → 0.2.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.
Files changed (177) hide show
  1. package/README.md +458 -93
  2. package/dist/.audit-log.jsonl +55 -0
  3. package/dist/.screenhand/memory/.lock +1 -0
  4. package/dist/.screenhand/memory/actions.jsonl +85 -0
  5. package/dist/.screenhand/memory/errors.jsonl +5 -0
  6. package/dist/.screenhand/memory/errors.jsonl.bak +4 -0
  7. package/dist/.screenhand/memory/state.json +35 -0
  8. package/dist/.screenhand/memory/state.json.bak +35 -0
  9. package/dist/.screenhand/memory/strategies.jsonl +12 -0
  10. package/dist/agent/cli.js +73 -0
  11. package/dist/agent/loop.js +258 -0
  12. package/dist/config.js +9 -0
  13. package/dist/index.js +56 -0
  14. package/dist/logging/timeline-logger.js +29 -0
  15. package/dist/mcp/mcp-stdio-server.js +448 -0
  16. package/dist/mcp/server.js +347 -0
  17. package/dist/mcp-desktop.js +2731 -0
  18. package/dist/mcp-entry.js +59 -0
  19. package/dist/memory/recall.js +160 -0
  20. package/dist/memory/research.js +98 -0
  21. package/dist/memory/seeds.js +89 -0
  22. package/dist/memory/session.js +161 -0
  23. package/dist/memory/store.js +391 -0
  24. package/dist/memory/types.js +4 -0
  25. package/dist/monitor/codex-monitor.js +377 -0
  26. package/dist/monitor/task-queue.js +84 -0
  27. package/dist/monitor/types.js +49 -0
  28. package/dist/native/bridge-client.js +174 -0
  29. package/dist/native/macos-bridge-client.js +5 -0
  30. package/dist/npm-publish-helper.js +117 -0
  31. package/dist/npm-token-cdp.js +113 -0
  32. package/dist/npm-token-create.js +135 -0
  33. package/dist/npm-token-finish.js +126 -0
  34. package/dist/playbook/engine.js +193 -0
  35. package/dist/playbook/index.js +4 -0
  36. package/dist/playbook/recorder.js +519 -0
  37. package/dist/playbook/runner.js +392 -0
  38. package/dist/playbook/store.js +166 -0
  39. package/dist/playbook/types.js +4 -0
  40. package/dist/runtime/accessibility-adapter.js +377 -0
  41. package/dist/runtime/app-adapter.js +48 -0
  42. package/dist/runtime/applescript-adapter.js +283 -0
  43. package/dist/runtime/ax-role-map.js +80 -0
  44. package/dist/runtime/browser-adapter.js +36 -0
  45. package/dist/runtime/cdp-chrome-adapter.js +505 -0
  46. package/dist/runtime/composite-adapter.js +205 -0
  47. package/dist/runtime/executor.js +250 -0
  48. package/dist/runtime/locator-cache.js +12 -0
  49. package/dist/runtime/planning-loop.js +47 -0
  50. package/dist/runtime/service.js +372 -0
  51. package/dist/runtime/session-manager.js +28 -0
  52. package/dist/runtime/state-observer.js +105 -0
  53. package/dist/runtime/vision-adapter.js +208 -0
  54. package/dist/scripts/codex-monitor-daemon.js +335 -0
  55. package/dist/scripts/supervisor-daemon.js +272 -0
  56. package/dist/scripts/worker-daemon.js +228 -0
  57. package/dist/src/agent/cli.js +82 -0
  58. package/dist/src/agent/loop.js +274 -0
  59. package/{src/config.ts → dist/src/config.js} +5 -10
  60. package/{src/index.ts → dist/src/index.js} +32 -52
  61. package/dist/src/jobs/manager.js +237 -0
  62. package/dist/src/jobs/runner.js +683 -0
  63. package/dist/src/jobs/store.js +102 -0
  64. package/dist/src/jobs/types.js +30 -0
  65. package/dist/src/jobs/worker.js +97 -0
  66. package/dist/src/logging/timeline-logger.js +45 -0
  67. package/dist/src/mcp/mcp-stdio-server.js +464 -0
  68. package/dist/src/mcp/server.js +363 -0
  69. package/dist/src/mcp-entry.js +60 -0
  70. package/dist/src/memory/recall.js +170 -0
  71. package/dist/src/memory/research.js +104 -0
  72. package/dist/src/memory/seeds.js +101 -0
  73. package/dist/src/memory/service.js +421 -0
  74. package/dist/src/memory/session.js +169 -0
  75. package/dist/src/memory/store.js +422 -0
  76. package/dist/src/memory/types.js +17 -0
  77. package/dist/src/monitor/codex-monitor.js +382 -0
  78. package/dist/src/monitor/task-queue.js +97 -0
  79. package/dist/src/monitor/types.js +62 -0
  80. package/dist/src/native/bridge-client.js +190 -0
  81. package/{src/native/macos-bridge-client.ts → dist/src/native/macos-bridge-client.js} +0 -1
  82. package/dist/src/playbook/engine.js +201 -0
  83. package/dist/src/playbook/index.js +20 -0
  84. package/dist/src/playbook/recorder.js +535 -0
  85. package/dist/src/playbook/runner.js +408 -0
  86. package/dist/src/playbook/store.js +183 -0
  87. package/dist/src/playbook/types.js +17 -0
  88. package/dist/src/runtime/accessibility-adapter.js +393 -0
  89. package/dist/src/runtime/app-adapter.js +64 -0
  90. package/dist/src/runtime/applescript-adapter.js +299 -0
  91. package/dist/src/runtime/ax-role-map.js +96 -0
  92. package/dist/src/runtime/browser-adapter.js +52 -0
  93. package/dist/src/runtime/cdp-chrome-adapter.js +521 -0
  94. package/dist/src/runtime/composite-adapter.js +221 -0
  95. package/dist/src/runtime/execution-contract.js +159 -0
  96. package/dist/src/runtime/executor.js +266 -0
  97. package/{src/runtime/locator-cache.ts → dist/src/runtime/locator-cache.js} +10 -15
  98. package/dist/src/runtime/planning-loop.js +63 -0
  99. package/dist/src/runtime/service.js +388 -0
  100. package/dist/src/runtime/session-manager.js +60 -0
  101. package/dist/src/runtime/state-observer.js +121 -0
  102. package/dist/src/runtime/vision-adapter.js +224 -0
  103. package/dist/src/supervisor/locks.js +186 -0
  104. package/dist/src/supervisor/supervisor.js +403 -0
  105. package/dist/src/supervisor/types.js +30 -0
  106. package/dist/src/test-mcp-protocol.js +154 -0
  107. package/dist/src/types.js +17 -0
  108. package/dist/src/util/atomic-write.js +118 -0
  109. package/dist/test-mcp-protocol.js +138 -0
  110. package/dist/types.js +1 -0
  111. package/package.json +18 -4
  112. package/.claude/commands/automate.md +0 -28
  113. package/.claude/commands/debug-ui.md +0 -19
  114. package/.claude/commands/screenshot.md +0 -15
  115. package/.github/FUNDING.yml +0 -1
  116. package/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
  117. package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
  118. package/.mcp.json +0 -8
  119. package/DESKTOP_MCP_GUIDE.md +0 -92
  120. package/SECURITY.md +0 -44
  121. package/docs/architecture.md +0 -47
  122. package/install-skills.sh +0 -19
  123. package/mcp-bridge.ts +0 -271
  124. package/mcp-desktop.ts +0 -1221
  125. package/native/macos-bridge/Package.swift +0 -21
  126. package/native/macos-bridge/Sources/AccessibilityBridge.swift +0 -261
  127. package/native/macos-bridge/Sources/AppManagement.swift +0 -129
  128. package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +0 -242
  129. package/native/macos-bridge/Sources/ObserverBridge.swift +0 -120
  130. package/native/macos-bridge/Sources/VisionBridge.swift +0 -80
  131. package/native/macos-bridge/Sources/main.swift +0 -345
  132. package/native/windows-bridge/AppManagement.cs +0 -234
  133. package/native/windows-bridge/InputBridge.cs +0 -436
  134. package/native/windows-bridge/Program.cs +0 -265
  135. package/native/windows-bridge/ScreenCapture.cs +0 -329
  136. package/native/windows-bridge/UIAutomationBridge.cs +0 -571
  137. package/native/windows-bridge/WindowsBridge.csproj +0 -17
  138. package/playbooks/devpost.json +0 -186
  139. package/playbooks/instagram.json +0 -41
  140. package/playbooks/instagram_v2.json +0 -201
  141. package/playbooks/x_v1.json +0 -211
  142. package/scripts/devpost-live-loop.mjs +0 -421
  143. package/src/logging/timeline-logger.ts +0 -55
  144. package/src/mcp/server.ts +0 -449
  145. package/src/memory/recall.ts +0 -191
  146. package/src/memory/research.ts +0 -146
  147. package/src/memory/seeds.ts +0 -123
  148. package/src/memory/session.ts +0 -201
  149. package/src/memory/store.ts +0 -434
  150. package/src/memory/types.ts +0 -69
  151. package/src/native/bridge-client.ts +0 -239
  152. package/src/runtime/accessibility-adapter.ts +0 -487
  153. package/src/runtime/app-adapter.ts +0 -169
  154. package/src/runtime/applescript-adapter.ts +0 -376
  155. package/src/runtime/ax-role-map.ts +0 -102
  156. package/src/runtime/browser-adapter.ts +0 -129
  157. package/src/runtime/cdp-chrome-adapter.ts +0 -676
  158. package/src/runtime/composite-adapter.ts +0 -274
  159. package/src/runtime/executor.ts +0 -396
  160. package/src/runtime/planning-loop.ts +0 -81
  161. package/src/runtime/service.ts +0 -448
  162. package/src/runtime/session-manager.ts +0 -50
  163. package/src/runtime/state-observer.ts +0 -136
  164. package/src/runtime/vision-adapter.ts +0 -297
  165. package/src/types.ts +0 -297
  166. package/tests/bridge-client.test.ts +0 -176
  167. package/tests/browser-stealth.test.ts +0 -210
  168. package/tests/composite-adapter.test.ts +0 -64
  169. package/tests/mcp-server.test.ts +0 -151
  170. package/tests/memory-recall.test.ts +0 -339
  171. package/tests/memory-research.test.ts +0 -159
  172. package/tests/memory-seeds.test.ts +0 -120
  173. package/tests/memory-store.test.ts +0 -392
  174. package/tests/types.test.ts +0 -92
  175. package/tsconfig.check.json +0 -17
  176. package/tsconfig.json +0 -19
  177. package/vitest.config.ts +0 -8
@@ -0,0 +1,683 @@
1
+ // Copyright (C) 2025 Clazro Technology Private Limited
2
+ // SPDX-License-Identifier: AGPL-3.0-only
3
+ //
4
+ // This file is part of ScreenHand.
5
+ //
6
+ // ScreenHand is free software: you can redistribute it and/or modify
7
+ // it under the terms of the GNU Affero General Public License as
8
+ // published by the Free Software Foundation, version 3.
9
+ //
10
+ // ScreenHand is distributed in the hope that it will be useful,
11
+ // but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
+ // GNU Affero General Public License for more details.
14
+ //
15
+ // You should have received a copy of the GNU Affero General Public License
16
+ // along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
17
+ import { planExecution, executeWithFallback, DEFAULT_RETRY_POLICY, } from "../runtime/execution-contract.js";
18
+ /** Patterns that indicate a blocker requiring human intervention. */
19
+ const HUMAN_BLOCKER_PATTERNS = [
20
+ "captcha", "recaptcha", "hcaptcha",
21
+ "2fa", "two-factor", "verification code",
22
+ "sign in", "log in", "login required",
23
+ "permission denied", "access denied",
24
+ "approve this", "confirm your identity",
25
+ ];
26
+ /** Patterns that indicate a transient blocker (auto-recoverable). */
27
+ const TRANSIENT_BLOCKER_PATTERNS = [
28
+ "rate limit", "too many requests", "try again later",
29
+ "loading", "please wait",
30
+ "timed out", "timeout",
31
+ "network error", "connection refused",
32
+ ];
33
+ const DEFAULT_CONFIG = {
34
+ heartbeatMs: 30_000,
35
+ stepDelayMs: 500,
36
+ maxConsecutiveFailures: 3,
37
+ hasCDP: false,
38
+ onLog: (msg) => console.error(`[JobRunner] ${msg}`),
39
+ };
40
+ export class JobRunner {
41
+ bridge;
42
+ jobs;
43
+ leaseManager;
44
+ supervisor;
45
+ config;
46
+ heartbeatTimer = null;
47
+ stopped = false;
48
+ constructor(bridge, jobs, leaseManager, supervisor, config) {
49
+ this.bridge = bridge;
50
+ this.jobs = jobs;
51
+ this.leaseManager = leaseManager;
52
+ this.supervisor = supervisor;
53
+ this.config = { ...DEFAULT_CONFIG, ...config };
54
+ }
55
+ log(msg) {
56
+ this.config.onLog?.(msg);
57
+ }
58
+ /**
59
+ * Run a single job cycle: dequeue → execute → finalize.
60
+ * Returns null if no jobs are queued.
61
+ */
62
+ async run() {
63
+ // 1. Dequeue
64
+ const job = this.jobs.dequeue();
65
+ if (!job)
66
+ return null;
67
+ const start = Date.now();
68
+ this.log(`Dequeued job ${job.id}: "${job.task}" (${job.steps.length} steps, resume from ${job.lastStep + 1})`);
69
+ // 2. Claim session
70
+ const sessionId = await this.claimSession(job);
71
+ if (!sessionId) {
72
+ this.jobs.transition(job.id, "failed", { error: "Failed to claim supervisor session" });
73
+ return { jobId: job.id, finalState: "failed", stepsCompleted: 0, totalSteps: job.steps.length, durationMs: Date.now() - start, error: "Failed to claim session" };
74
+ }
75
+ // 3. Start heartbeat
76
+ this.startHeartbeat(sessionId);
77
+ try {
78
+ // 4. Route: playbook engine or free-form steps
79
+ if (job.playbookId) {
80
+ if (!this.config.playbookEngine || !this.config.playbookStore) {
81
+ const err = `Job requires playbook "${job.playbookId}" but no playbook engine is configured`;
82
+ this.jobs.transition(job.id, "failed", { error: err });
83
+ return this.finalize(job, start, 0, err);
84
+ }
85
+ return await this.runViaPlaybookEngine(job, sessionId, start);
86
+ }
87
+ return await this.runFreeFormSteps(job, start);
88
+ }
89
+ catch (err) {
90
+ const msg = err instanceof Error ? err.message : String(err);
91
+ this.jobs.transition(job.id, "failed", { error: msg });
92
+ this.log(`Job ${job.id} → failed (unhandled): ${msg}`);
93
+ return this.finalize(job, start, 0, msg);
94
+ }
95
+ finally {
96
+ this.stopHeartbeat();
97
+ this.releaseSession(sessionId);
98
+ }
99
+ }
100
+ // ── Playbook engine path ──────────────────────
101
+ async runViaPlaybookEngine(job, sessionId, start) {
102
+ const engine = this.config.playbookEngine;
103
+ const store = this.config.playbookStore;
104
+ const playbook = store.get(job.playbookId);
105
+ if (!playbook) {
106
+ this.jobs.transition(job.id, "failed", { error: `Playbook "${job.playbookId}" not found` });
107
+ return this.finalize(job, start, 0, `Playbook "${job.playbookId}" not found`);
108
+ }
109
+ this.log(` Using playbook engine: "${playbook.name}" (${playbook.steps.length} steps)`);
110
+ // If job has no steps yet, populate from playbook so step tracking works
111
+ if (job.steps.length === 0 && playbook.steps.length > 0) {
112
+ for (let i = 0; i < playbook.steps.length; i++) {
113
+ const ps = playbook.steps[i];
114
+ const step = { index: i, action: ps.action, status: "pending" };
115
+ const target = typeof ps.target === "string" ? ps.target : ps.target ? JSON.stringify(ps.target) : undefined;
116
+ if (target !== undefined)
117
+ step.target = target;
118
+ if (ps.description !== undefined)
119
+ step.description = ps.description;
120
+ if (ps.text !== undefined)
121
+ step.text = ps.text;
122
+ if (ps.keys)
123
+ step.keys = ps.keys.join("+");
124
+ job.steps.push(step);
125
+ }
126
+ }
127
+ // Build remaining-steps playbook (resume from lastStep+1)
128
+ const resumeIdx = job.lastStep + 1;
129
+ const remainingSteps = playbook.steps.slice(resumeIdx);
130
+ if (remainingSteps.length === 0) {
131
+ this.jobs.transition(job.id, "done");
132
+ return this.finalize(job, start, playbook.steps.length, null);
133
+ }
134
+ const remainingPlaybook = {
135
+ ...playbook,
136
+ id: `${playbook.id}_job_${job.id}`,
137
+ steps: remainingSteps,
138
+ };
139
+ // Create a runtime session if available, focus target app
140
+ let runtimeSessionId = null;
141
+ if (this.config.runtimeService) {
142
+ try {
143
+ const session = await this.config.runtimeService.sessionStart("jobrunner");
144
+ runtimeSessionId = session.sessionId;
145
+ // Focus target app if specified
146
+ if (job.bundleId) {
147
+ await this.config.runtimeService.appFocus({ sessionId: session.sessionId, bundleId: job.bundleId });
148
+ }
149
+ }
150
+ catch (err) {
151
+ this.log(` Warning: failed to create runtime session: ${err instanceof Error ? err.message : String(err)}`);
152
+ }
153
+ }
154
+ const engineSessionId = runtimeSessionId ?? sessionId;
155
+ let stepsCompleted = 0;
156
+ const result = await engine.run(engineSessionId, remainingPlaybook, {
157
+ onStep: (i, step, res) => {
158
+ const globalIdx = resumeIdx + i;
159
+ this.jobs.completeStep(job.id, globalIdx, { durationMs: 0 });
160
+ stepsCompleted++;
161
+ this.log(` Step ${globalIdx}/${playbook.steps.length - 1}: ${step.description ?? step.action} → ${res}`);
162
+ },
163
+ });
164
+ if (result.success) {
165
+ store.recordOutcome(playbook.id, true);
166
+ this.jobs.transition(job.id, "done");
167
+ this.log(`Job ${job.id} → done via playbook engine (${stepsCompleted} steps in ${result.durationMs}ms)`);
168
+ }
169
+ else {
170
+ store.recordOutcome(playbook.id, false);
171
+ const error = result.error ?? `Playbook failed at step ${result.failedAtStep}`;
172
+ // Mark the failed step
173
+ if (result.failedAtStep >= 0) {
174
+ const globalFailIdx = resumeIdx + result.failedAtStep;
175
+ this.jobs.failStep(job.id, globalFailIdx, error);
176
+ }
177
+ // Classify blocker from the error
178
+ const blocker = this.classifyBlocker(error);
179
+ if (blocker === "human") {
180
+ this.jobs.transition(job.id, "waiting_human", { blockReason: error });
181
+ }
182
+ else if (blocker === "transient") {
183
+ this.jobs.transition(job.id, "blocked", { blockReason: error });
184
+ }
185
+ else {
186
+ this.jobs.transition(job.id, "failed", { error });
187
+ }
188
+ this.log(`Job ${job.id} → ${blocker === "human" ? "waiting_human" : blocker === "transient" ? "blocked" : "failed"}: ${error}`);
189
+ }
190
+ return this.finalize(job, start, stepsCompleted, result.success ? null : (result.error ?? null));
191
+ }
192
+ // ── Free-form step execution path ─────────────
193
+ async runFreeFormSteps(job, start) {
194
+ let consecutiveFailures = 0;
195
+ let stepsCompleted = 0;
196
+ let lastError = null;
197
+ const resumeIdx = job.lastStep + 1;
198
+ for (let i = resumeIdx; i < job.steps.length; i++) {
199
+ if (this.stopped) {
200
+ this.log(`Runner stopped — pausing job ${job.id} at step ${i}`);
201
+ break;
202
+ }
203
+ const step = job.steps[i];
204
+ if (step.status === "done" || step.status === "skipped") {
205
+ stepsCompleted++;
206
+ continue;
207
+ }
208
+ // Focus/validate target app before each step
209
+ await this.focusTargetApp(job);
210
+ this.log(` Step ${i}/${job.steps.length - 1}: ${step.description ?? step.action}${step.target ? ` → "${step.target}"` : ""}`);
211
+ const stepStart = Date.now();
212
+ const result = await this.executeStep(step);
213
+ if (result.ok) {
214
+ this.jobs.completeStep(job.id, i, { durationMs: Date.now() - stepStart });
215
+ stepsCompleted++;
216
+ consecutiveFailures = 0;
217
+ this.log(` ✓ ${result.method} in ${result.durationMs}ms${result.fallbackFrom ? ` (fallback from ${result.fallbackFrom})` : ""}`);
218
+ }
219
+ else {
220
+ consecutiveFailures++;
221
+ lastError = result.error ?? "Unknown error";
222
+ this.jobs.failStep(job.id, i, lastError);
223
+ this.log(` ✗ ${lastError}`);
224
+ // Check for blocker patterns across all errors from the fallback chain
225
+ const { type: blocker, matchedError: blockerError } = this.classifyBlockerFromErrors(this.lastStepErrors);
226
+ if (blocker === "human") {
227
+ const reason = blockerError ?? lastError;
228
+ this.jobs.transition(job.id, "waiting_human", { blockReason: reason });
229
+ this.log(` → waiting_human: ${reason}`);
230
+ return this.finalize(job, start, stepsCompleted, reason);
231
+ }
232
+ if (blocker === "transient") {
233
+ const reason = blockerError ?? lastError;
234
+ this.jobs.transition(job.id, "blocked", { blockReason: reason });
235
+ this.log(` → blocked (transient): ${reason}`);
236
+ return this.finalize(job, start, stepsCompleted, reason);
237
+ }
238
+ if (consecutiveFailures >= this.config.maxConsecutiveFailures) {
239
+ this.jobs.transition(job.id, "failed", { error: `${consecutiveFailures} consecutive step failures. Last: ${lastError}` });
240
+ this.log(` → failed: ${consecutiveFailures} consecutive failures`);
241
+ return this.finalize(job, start, stepsCompleted, lastError);
242
+ }
243
+ }
244
+ // Delay between steps
245
+ if (i < job.steps.length - 1) {
246
+ await delay(this.config.stepDelayMs);
247
+ }
248
+ }
249
+ // Check if all steps complete
250
+ const updated = this.jobs.get(job.id);
251
+ if (!updated) {
252
+ return this.finalize(job, start, stepsCompleted, "Job disappeared");
253
+ }
254
+ const allDone = updated.steps.every((s) => s.status === "done" || s.status === "skipped");
255
+ if (allDone && !this.stopped) {
256
+ this.jobs.transition(job.id, "done");
257
+ this.log(`Job ${job.id} → done (${stepsCompleted} steps in ${Date.now() - start}ms)`);
258
+ }
259
+ else if (this.stopped) {
260
+ this.log(`Job ${job.id} paused at step ${updated.lastStep + 1}`);
261
+ }
262
+ return this.finalize(job, start, stepsCompleted, lastError);
263
+ }
264
+ // ── Target app focus ──────────────────────────
265
+ /**
266
+ * Focus the job's target bundleId/windowId before acting.
267
+ * Validates the app is still running. Skips if no bundleId set.
268
+ */
269
+ async focusTargetApp(job) {
270
+ if (!job.bundleId)
271
+ return;
272
+ try {
273
+ // Verify the app is running
274
+ const apps = await this.bridge.call("app.list");
275
+ const target = apps.find((a) => a.bundleId === job.bundleId);
276
+ if (!target) {
277
+ throw new Error(`Target app ${job.bundleId} is not running`);
278
+ }
279
+ // Focus the app
280
+ await this.bridge.call("app.focus", { bundleId: job.bundleId });
281
+ // If windowId specified, validate it exists
282
+ if (job.windowId != null) {
283
+ const wins = await this.bridge.call("app.windows");
284
+ const targetWin = wins.find((w) => w.windowId === job.windowId && w.pid === target.pid);
285
+ if (!targetWin) {
286
+ this.log(` Warning: window ${job.windowId} not found for ${job.bundleId}, using frontmost`);
287
+ }
288
+ }
289
+ }
290
+ catch (err) {
291
+ // Log but don't fail — the step itself will fail if the target isn't right
292
+ this.log(` Warning: focus target app failed: ${err instanceof Error ? err.message : String(err)}`);
293
+ }
294
+ }
295
+ /**
296
+ * Continuous loop: process jobs until stop() is called or queue is empty.
297
+ * Returns when stopped or no more queued jobs.
298
+ */
299
+ async runLoop() {
300
+ const results = [];
301
+ this.stopped = false;
302
+ while (!this.stopped) {
303
+ const result = await this.run();
304
+ if (!result)
305
+ break; // Queue empty
306
+ results.push(result);
307
+ }
308
+ return results;
309
+ }
310
+ /** Signal the runner to stop after the current step. */
311
+ stop() {
312
+ this.stopped = true;
313
+ }
314
+ // ── Session management ──────────────────────────
315
+ async claimSession(job) {
316
+ // If job already has a session, verify it's still valid
317
+ if (job.sessionId) {
318
+ const ok = this.supervisor
319
+ ? this.supervisor.heartbeat(job.sessionId)
320
+ : this.leaseManager.heartbeat(job.sessionId);
321
+ if (ok)
322
+ return job.sessionId;
323
+ // Session expired — claim a new one
324
+ }
325
+ const client = { id: `jobrunner_${job.id}`, type: "jobrunner", startedAt: new Date().toISOString() };
326
+ const app = job.bundleId ?? "com.screenhand.jobrunner";
327
+ const windowId = job.windowId ?? 0;
328
+ try {
329
+ let sessionId = null;
330
+ if (this.supervisor) {
331
+ // Use supervisor path — inherits stall detection + recovery
332
+ const lease = this.supervisor.registerSession(client, app, windowId);
333
+ sessionId = lease?.sessionId ?? null;
334
+ }
335
+ else {
336
+ // Fallback to raw lease manager
337
+ const lease = this.leaseManager.claim(client, app, windowId);
338
+ sessionId = lease?.sessionId ?? null;
339
+ }
340
+ if (!sessionId)
341
+ return null;
342
+ // Bind session to job
343
+ this.jobs.transition(job.id, "running", { sessionId });
344
+ return sessionId;
345
+ }
346
+ catch {
347
+ return null;
348
+ }
349
+ }
350
+ startHeartbeat(sessionId) {
351
+ this.stopHeartbeat();
352
+ this.heartbeatTimer = setInterval(() => {
353
+ if (this.supervisor) {
354
+ this.supervisor.heartbeat(sessionId);
355
+ }
356
+ else {
357
+ this.leaseManager.heartbeat(sessionId);
358
+ }
359
+ }, this.config.heartbeatMs);
360
+ }
361
+ stopHeartbeat() {
362
+ if (this.heartbeatTimer) {
363
+ clearInterval(this.heartbeatTimer);
364
+ this.heartbeatTimer = null;
365
+ }
366
+ }
367
+ releaseSession(sessionId) {
368
+ try {
369
+ if (this.supervisor) {
370
+ this.supervisor.releaseSession(sessionId);
371
+ }
372
+ else {
373
+ this.leaseManager.release(sessionId);
374
+ }
375
+ }
376
+ catch {
377
+ // Best-effort
378
+ }
379
+ }
380
+ // ── Step execution ──────────────────────────────
381
+ /** All errors collected during the last executeStep call (across fallback methods). */
382
+ lastStepErrors = [];
383
+ async executeStep(step) {
384
+ const actionType = this.mapActionType(step.action);
385
+ const infra = { hasBridge: true, hasCDP: this.config.hasCDP };
386
+ const plan = planExecution(actionType, infra);
387
+ this.lastStepErrors = [];
388
+ if (plan.length === 0) {
389
+ return { ok: false, method: "ax", durationMs: 0, fallbackFrom: null, retries: 0, error: `No execution method available for "${step.action}"`, target: step.target ?? null };
390
+ }
391
+ return executeWithFallback(step.action, plan, DEFAULT_RETRY_POLICY, async (method, attempt) => {
392
+ const result = await this.executeViaMethod(method, step, attempt);
393
+ if (!result.ok && result.error)
394
+ this.lastStepErrors.push(result.error);
395
+ return result;
396
+ });
397
+ }
398
+ async executeViaMethod(method, step, attempt) {
399
+ const start = Date.now();
400
+ const target = step.target ?? null;
401
+ try {
402
+ switch (step.action) {
403
+ case "click":
404
+ case "press":
405
+ return await this.execClick(method, target, start, attempt);
406
+ case "type_text":
407
+ case "type_into":
408
+ case "type":
409
+ return await this.execType(method, target, step.text ?? step.description ?? "", start, attempt);
410
+ case "navigate":
411
+ return await this.execNavigate(target, start, attempt);
412
+ case "screenshot":
413
+ return await this.execScreenshot(start, attempt);
414
+ case "scroll":
415
+ return await this.execScroll(method, step.description ?? "down", start, attempt);
416
+ case "wait":
417
+ await delay(1000);
418
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: "wait" };
419
+ case "key_combo":
420
+ case "key":
421
+ return await this.execKey(step.keys ?? target ?? "", start, attempt);
422
+ case "read":
423
+ case "extract":
424
+ return await this.execRead(method, target, start, attempt);
425
+ default:
426
+ // Try as a generic click on the target text
427
+ if (target)
428
+ return await this.execClick(method, target, start, attempt);
429
+ return { ok: false, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: `Unknown action: ${step.action}`, target };
430
+ }
431
+ }
432
+ catch (err) {
433
+ return { ok: false, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: err instanceof Error ? err.message : String(err), target };
434
+ }
435
+ }
436
+ // ── Bridge execution methods ────────────────────
437
+ async execClick(method, target, start, attempt) {
438
+ if (!target)
439
+ return { ok: false, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: "Click requires a target", target };
440
+ switch (method) {
441
+ case "ax": {
442
+ const found = await this.bridge.call("ax.findElement", { pid: 0, title: target, exact: false });
443
+ await this.bridge.call("ax.performAction", { pid: 0, elementPath: found.elementPath, action: "AXPress" });
444
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target };
445
+ }
446
+ case "cdp": {
447
+ if (!this.config.cdpConnect)
448
+ throw new Error("CDP not available");
449
+ const client = await this.config.cdpConnect();
450
+ try {
451
+ const evalResult = await client.Runtime.evaluate({
452
+ expression: `(() => { const el = Array.from(document.querySelectorAll('*')).find(e => e.textContent?.trim() === ${JSON.stringify(target)} || e.getAttribute('aria-label') === ${JSON.stringify(target)}); if (el) { el.click(); return 'clicked'; } return null; })()`,
453
+ returnByValue: true,
454
+ });
455
+ if (evalResult.result?.value !== "clicked")
456
+ throw new Error("Element not found via CDP");
457
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target };
458
+ }
459
+ finally {
460
+ await client.close();
461
+ }
462
+ }
463
+ case "ocr": {
464
+ const shot = await this.bridge.call("cg.captureScreen", {});
465
+ const matches = await this.bridge.call("vision.findText", { imagePath: shot.path, searchText: target });
466
+ const match = Array.isArray(matches) ? matches[0] : null;
467
+ if (!match?.bounds)
468
+ throw new Error("Target not found via OCR");
469
+ const x = match.bounds.x + match.bounds.width / 2;
470
+ const y = match.bounds.y + match.bounds.height / 2;
471
+ await this.bridge.call("cg.mouseClick", { x, y });
472
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target };
473
+ }
474
+ case "coordinates": {
475
+ // Can't click by text with coordinates alone — need a prior locate
476
+ throw new Error("Coordinate click requires explicit x,y — not available for text target");
477
+ }
478
+ }
479
+ throw new Error(`Unknown method: ${method}`);
480
+ }
481
+ async execType(method, target, text, start, attempt) {
482
+ switch (method) {
483
+ case "ax": {
484
+ if (target) {
485
+ const found = await this.bridge.call("ax.findElement", { pid: 0, title: target, exact: false });
486
+ await this.bridge.call("ax.setElementValue", { pid: 0, elementPath: found.elementPath, value: text });
487
+ }
488
+ else {
489
+ // Type into focused element via key events
490
+ await this.bridge.call("cg.typeText", { text });
491
+ }
492
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target };
493
+ }
494
+ case "cdp": {
495
+ if (!this.config.cdpConnect)
496
+ throw new Error("CDP not available");
497
+ const client = await this.config.cdpConnect();
498
+ try {
499
+ if (target) {
500
+ const evalResult = await client.Runtime.evaluate({
501
+ expression: `(() => { const el = Array.from(document.querySelectorAll('input, textarea, [contenteditable]')).find(e => e.getAttribute('placeholder') === ${JSON.stringify(target)} || e.getAttribute('aria-label') === ${JSON.stringify(target)} || e.getAttribute('name') === ${JSON.stringify(target)}); if (el) { el.focus(); return true; } return false; })()`,
502
+ returnByValue: true,
503
+ });
504
+ if (!evalResult.result?.value)
505
+ throw new Error("Field not found via CDP");
506
+ }
507
+ for (const char of text) {
508
+ await client.Input.dispatchKeyEvent({ type: "keyDown", key: char, text: char });
509
+ await client.Input.dispatchKeyEvent({ type: "keyUp", key: char });
510
+ }
511
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target };
512
+ }
513
+ finally {
514
+ await client.close();
515
+ }
516
+ }
517
+ }
518
+ throw new Error(`Method ${method} does not support type`);
519
+ }
520
+ async execNavigate(url, start, attempt) {
521
+ if (!url)
522
+ return { ok: false, method: "ax", durationMs: 0, fallbackFrom: null, retries: attempt, error: "Navigate requires a URL target", target: null };
523
+ if (this.config.cdpConnect) {
524
+ const client = await this.config.cdpConnect();
525
+ try {
526
+ await client.Runtime.evaluate({ expression: `window.location.href = ${JSON.stringify(url)}` });
527
+ return { ok: true, method: "cdp", durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: url };
528
+ }
529
+ finally {
530
+ await client.close();
531
+ }
532
+ }
533
+ // Fallback: use AppleScript / open command
534
+ await this.bridge.call("app.openURL", { url });
535
+ return { ok: true, method: "ax", durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: url };
536
+ }
537
+ async execScreenshot(start, attempt) {
538
+ const shot = await this.bridge.call("cg.captureScreen", {});
539
+ return { ok: true, method: "ax", durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: shot.path };
540
+ }
541
+ async execScroll(method, direction, start, attempt) {
542
+ const amount = 300;
543
+ const deltaX = direction === "left" ? -amount : direction === "right" ? amount : 0;
544
+ const deltaY = direction === "up" ? -amount : direction === "down" ? amount : 0;
545
+ switch (method) {
546
+ case "ax":
547
+ case "coordinates":
548
+ await this.bridge.call("cg.scroll", { deltaX, deltaY });
549
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: `${direction} ${amount}px` };
550
+ case "cdp": {
551
+ if (!this.config.cdpConnect)
552
+ throw new Error("CDP not available");
553
+ const client = await this.config.cdpConnect();
554
+ try {
555
+ await client.Runtime.evaluate({ expression: `window.scrollBy(${deltaX}, ${deltaY})` });
556
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: `${direction} ${amount}px` };
557
+ }
558
+ finally {
559
+ await client.close();
560
+ }
561
+ }
562
+ }
563
+ throw new Error(`Method ${method} does not support scroll`);
564
+ }
565
+ async execKey(keys, start, attempt) {
566
+ // keys is a "+" separated combo like "cmd+a"
567
+ const parts = keys.split("+").map((k) => k.trim());
568
+ await this.bridge.call("cg.keyCombo", { keys: parts });
569
+ return { ok: true, method: "ax", durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: keys };
570
+ }
571
+ async execRead(method, target, start, attempt) {
572
+ switch (method) {
573
+ case "ax": {
574
+ if (target) {
575
+ const found = await this.bridge.call("ax.findElement", { pid: 0, title: target, exact: false });
576
+ const val = await this.bridge.call("ax.getElementValue", { pid: 0, elementPath: found.elementPath });
577
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: val.value ?? "" };
578
+ }
579
+ const tree = await this.bridge.call("ax.getElementTree", { pid: 0, maxDepth: 4 });
580
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: tree.description ?? "" };
581
+ }
582
+ case "cdp": {
583
+ if (!this.config.cdpConnect)
584
+ throw new Error("CDP not available");
585
+ const client = await this.config.cdpConnect();
586
+ try {
587
+ if (target) {
588
+ const evalResult = await client.Runtime.evaluate({
589
+ expression: `(() => { const el = Array.from(document.querySelectorAll('*')).find(e => e.getAttribute('aria-label') === ${JSON.stringify(target)} || e.textContent?.trim() === ${JSON.stringify(target)}); return el ? (el.value ?? el.textContent ?? '').trim() : null; })()`,
590
+ returnByValue: true,
591
+ });
592
+ if (evalResult.result?.value == null)
593
+ throw new Error("Element not found via CDP");
594
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: String(evalResult.result.value) };
595
+ }
596
+ const evalResult = await client.Runtime.evaluate({
597
+ expression: "document.body?.innerText?.slice(0, 4000) ?? ''",
598
+ returnByValue: true,
599
+ });
600
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: String(evalResult.result?.value ?? "") };
601
+ }
602
+ finally {
603
+ await client.close();
604
+ }
605
+ }
606
+ case "ocr": {
607
+ const shot = await this.bridge.call("cg.captureScreen", {});
608
+ if (target) {
609
+ const matches = await this.bridge.call("vision.findText", { imagePath: shot.path, searchText: target });
610
+ const match = Array.isArray(matches) ? matches[0] : null;
611
+ if (!match)
612
+ throw new Error("Text not found via OCR");
613
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: match.text };
614
+ }
615
+ const ocr = await this.bridge.call("vision.ocr", { imagePath: shot.path });
616
+ return { ok: true, method, durationMs: Date.now() - start, fallbackFrom: null, retries: attempt, error: null, target: ocr.text?.slice(0, 4000) ?? "" };
617
+ }
618
+ }
619
+ throw new Error(`Method ${method} does not support read`);
620
+ }
621
+ // ── Blocker classification ──────────────────────
622
+ /** Check a single error string for blocker patterns. */
623
+ classifyBlocker(error) {
624
+ const lower = error.toLowerCase();
625
+ for (const pattern of HUMAN_BLOCKER_PATTERNS) {
626
+ if (lower.includes(pattern))
627
+ return "human";
628
+ }
629
+ for (const pattern of TRANSIENT_BLOCKER_PATTERNS) {
630
+ if (lower.includes(pattern))
631
+ return "transient";
632
+ }
633
+ return null;
634
+ }
635
+ /** Check all errors from a fallback chain — return the highest-priority blocker found with the matched error. */
636
+ classifyBlockerFromErrors(errors) {
637
+ let transientError = null;
638
+ for (const err of errors) {
639
+ const result = this.classifyBlocker(err);
640
+ if (result === "human")
641
+ return { type: "human", matchedError: err };
642
+ if (result === "transient" && !transientError)
643
+ transientError = err;
644
+ }
645
+ if (transientError)
646
+ return { type: "transient", matchedError: transientError };
647
+ return { type: null, matchedError: null };
648
+ }
649
+ // ── Helpers ─────────────────────────────────────
650
+ mapActionType(action) {
651
+ switch (action) {
652
+ case "click":
653
+ case "press": return "click";
654
+ case "type_text":
655
+ case "type_into":
656
+ case "type": return "type";
657
+ case "read":
658
+ case "extract": return "read";
659
+ case "scroll": return "scroll";
660
+ case "navigate":
661
+ case "screenshot":
662
+ case "wait":
663
+ case "key_combo":
664
+ case "key":
665
+ return "click"; // These don't go through the fallback chain — handled specially
666
+ default: return "click";
667
+ }
668
+ }
669
+ finalize(job, start, stepsCompleted, lastError) {
670
+ const updated = this.jobs.get(job.id);
671
+ return {
672
+ jobId: job.id,
673
+ finalState: updated?.state ?? "failed",
674
+ stepsCompleted,
675
+ totalSteps: job.steps.length,
676
+ durationMs: Date.now() - start,
677
+ error: lastError,
678
+ };
679
+ }
680
+ }
681
+ function delay(ms) {
682
+ return new Promise((resolve) => setTimeout(resolve, ms));
683
+ }