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.
- package/README.md +458 -93
- package/dist/.audit-log.jsonl +55 -0
- package/dist/.screenhand/memory/.lock +1 -0
- package/dist/.screenhand/memory/actions.jsonl +85 -0
- package/dist/.screenhand/memory/errors.jsonl +5 -0
- package/dist/.screenhand/memory/errors.jsonl.bak +4 -0
- package/dist/.screenhand/memory/state.json +35 -0
- package/dist/.screenhand/memory/state.json.bak +35 -0
- package/dist/.screenhand/memory/strategies.jsonl +12 -0
- package/dist/agent/cli.js +73 -0
- package/dist/agent/loop.js +258 -0
- package/dist/config.js +9 -0
- package/dist/index.js +56 -0
- package/dist/logging/timeline-logger.js +29 -0
- package/dist/mcp/mcp-stdio-server.js +448 -0
- package/dist/mcp/server.js +347 -0
- package/dist/mcp-desktop.js +2731 -0
- package/dist/mcp-entry.js +59 -0
- package/dist/memory/recall.js +160 -0
- package/dist/memory/research.js +98 -0
- package/dist/memory/seeds.js +89 -0
- package/dist/memory/session.js +161 -0
- package/dist/memory/store.js +391 -0
- package/dist/memory/types.js +4 -0
- package/dist/monitor/codex-monitor.js +377 -0
- package/dist/monitor/task-queue.js +84 -0
- package/dist/monitor/types.js +49 -0
- package/dist/native/bridge-client.js +174 -0
- package/dist/native/macos-bridge-client.js +5 -0
- package/dist/npm-publish-helper.js +117 -0
- package/dist/npm-token-cdp.js +113 -0
- package/dist/npm-token-create.js +135 -0
- package/dist/npm-token-finish.js +126 -0
- package/dist/playbook/engine.js +193 -0
- package/dist/playbook/index.js +4 -0
- package/dist/playbook/recorder.js +519 -0
- package/dist/playbook/runner.js +392 -0
- package/dist/playbook/store.js +166 -0
- package/dist/playbook/types.js +4 -0
- package/dist/runtime/accessibility-adapter.js +377 -0
- package/dist/runtime/app-adapter.js +48 -0
- package/dist/runtime/applescript-adapter.js +283 -0
- package/dist/runtime/ax-role-map.js +80 -0
- package/dist/runtime/browser-adapter.js +36 -0
- package/dist/runtime/cdp-chrome-adapter.js +505 -0
- package/dist/runtime/composite-adapter.js +205 -0
- package/dist/runtime/executor.js +250 -0
- package/dist/runtime/locator-cache.js +12 -0
- package/dist/runtime/planning-loop.js +47 -0
- package/dist/runtime/service.js +372 -0
- package/dist/runtime/session-manager.js +28 -0
- package/dist/runtime/state-observer.js +105 -0
- package/dist/runtime/vision-adapter.js +208 -0
- package/dist/scripts/codex-monitor-daemon.js +335 -0
- package/dist/scripts/supervisor-daemon.js +272 -0
- package/dist/scripts/worker-daemon.js +228 -0
- package/dist/src/agent/cli.js +82 -0
- package/dist/src/agent/loop.js +274 -0
- package/{src/config.ts → dist/src/config.js} +5 -10
- package/{src/index.ts → dist/src/index.js} +32 -52
- package/dist/src/jobs/manager.js +237 -0
- package/dist/src/jobs/runner.js +683 -0
- package/dist/src/jobs/store.js +102 -0
- package/dist/src/jobs/types.js +30 -0
- package/dist/src/jobs/worker.js +97 -0
- package/dist/src/logging/timeline-logger.js +45 -0
- package/dist/src/mcp/mcp-stdio-server.js +464 -0
- package/dist/src/mcp/server.js +363 -0
- package/dist/src/mcp-entry.js +60 -0
- package/dist/src/memory/recall.js +170 -0
- package/dist/src/memory/research.js +104 -0
- package/dist/src/memory/seeds.js +101 -0
- package/dist/src/memory/service.js +421 -0
- package/dist/src/memory/session.js +169 -0
- package/dist/src/memory/store.js +422 -0
- package/dist/src/memory/types.js +17 -0
- package/dist/src/monitor/codex-monitor.js +382 -0
- package/dist/src/monitor/task-queue.js +97 -0
- package/dist/src/monitor/types.js +62 -0
- package/dist/src/native/bridge-client.js +190 -0
- package/{src/native/macos-bridge-client.ts → dist/src/native/macos-bridge-client.js} +0 -1
- package/dist/src/playbook/engine.js +201 -0
- package/dist/src/playbook/index.js +20 -0
- package/dist/src/playbook/recorder.js +535 -0
- package/dist/src/playbook/runner.js +408 -0
- package/dist/src/playbook/store.js +183 -0
- package/dist/src/playbook/types.js +17 -0
- package/dist/src/runtime/accessibility-adapter.js +393 -0
- package/dist/src/runtime/app-adapter.js +64 -0
- package/dist/src/runtime/applescript-adapter.js +299 -0
- package/dist/src/runtime/ax-role-map.js +96 -0
- package/dist/src/runtime/browser-adapter.js +52 -0
- package/dist/src/runtime/cdp-chrome-adapter.js +521 -0
- package/dist/src/runtime/composite-adapter.js +221 -0
- package/dist/src/runtime/execution-contract.js +159 -0
- package/dist/src/runtime/executor.js +266 -0
- package/{src/runtime/locator-cache.ts → dist/src/runtime/locator-cache.js} +10 -15
- package/dist/src/runtime/planning-loop.js +63 -0
- package/dist/src/runtime/service.js +388 -0
- package/dist/src/runtime/session-manager.js +60 -0
- package/dist/src/runtime/state-observer.js +121 -0
- package/dist/src/runtime/vision-adapter.js +224 -0
- package/dist/src/supervisor/locks.js +186 -0
- package/dist/src/supervisor/supervisor.js +403 -0
- package/dist/src/supervisor/types.js +30 -0
- package/dist/src/test-mcp-protocol.js +154 -0
- package/dist/src/types.js +17 -0
- package/dist/src/util/atomic-write.js +118 -0
- package/dist/test-mcp-protocol.js +138 -0
- package/dist/types.js +1 -0
- package/package.json +18 -4
- package/.claude/commands/automate.md +0 -28
- package/.claude/commands/debug-ui.md +0 -19
- package/.claude/commands/screenshot.md +0 -15
- package/.github/FUNDING.yml +0 -1
- package/.github/ISSUE_TEMPLATE/bug_report.md +0 -27
- package/.github/ISSUE_TEMPLATE/feature_request.md +0 -20
- package/.mcp.json +0 -8
- package/DESKTOP_MCP_GUIDE.md +0 -92
- package/SECURITY.md +0 -44
- package/docs/architecture.md +0 -47
- package/install-skills.sh +0 -19
- package/mcp-bridge.ts +0 -271
- package/mcp-desktop.ts +0 -1221
- package/native/macos-bridge/Package.swift +0 -21
- package/native/macos-bridge/Sources/AccessibilityBridge.swift +0 -261
- package/native/macos-bridge/Sources/AppManagement.swift +0 -129
- package/native/macos-bridge/Sources/CoreGraphicsBridge.swift +0 -242
- package/native/macos-bridge/Sources/ObserverBridge.swift +0 -120
- package/native/macos-bridge/Sources/VisionBridge.swift +0 -80
- package/native/macos-bridge/Sources/main.swift +0 -345
- package/native/windows-bridge/AppManagement.cs +0 -234
- package/native/windows-bridge/InputBridge.cs +0 -436
- package/native/windows-bridge/Program.cs +0 -265
- package/native/windows-bridge/ScreenCapture.cs +0 -329
- package/native/windows-bridge/UIAutomationBridge.cs +0 -571
- package/native/windows-bridge/WindowsBridge.csproj +0 -17
- package/playbooks/devpost.json +0 -186
- package/playbooks/instagram.json +0 -41
- package/playbooks/instagram_v2.json +0 -201
- package/playbooks/x_v1.json +0 -211
- package/scripts/devpost-live-loop.mjs +0 -421
- package/src/logging/timeline-logger.ts +0 -55
- package/src/mcp/server.ts +0 -449
- package/src/memory/recall.ts +0 -191
- package/src/memory/research.ts +0 -146
- package/src/memory/seeds.ts +0 -123
- package/src/memory/session.ts +0 -201
- package/src/memory/store.ts +0 -434
- package/src/memory/types.ts +0 -69
- package/src/native/bridge-client.ts +0 -239
- package/src/runtime/accessibility-adapter.ts +0 -487
- package/src/runtime/app-adapter.ts +0 -169
- package/src/runtime/applescript-adapter.ts +0 -376
- package/src/runtime/ax-role-map.ts +0 -102
- package/src/runtime/browser-adapter.ts +0 -129
- package/src/runtime/cdp-chrome-adapter.ts +0 -676
- package/src/runtime/composite-adapter.ts +0 -274
- package/src/runtime/executor.ts +0 -396
- package/src/runtime/planning-loop.ts +0 -81
- package/src/runtime/service.ts +0 -448
- package/src/runtime/session-manager.ts +0 -50
- package/src/runtime/state-observer.ts +0 -136
- package/src/runtime/vision-adapter.ts +0 -297
- package/src/types.ts +0 -297
- package/tests/bridge-client.test.ts +0 -176
- package/tests/browser-stealth.test.ts +0 -210
- package/tests/composite-adapter.test.ts +0 -64
- package/tests/mcp-server.test.ts +0 -151
- package/tests/memory-recall.test.ts +0 -339
- package/tests/memory-research.test.ts +0 -159
- package/tests/memory-seeds.test.ts +0 -120
- package/tests/memory-store.test.ts +0 -392
- package/tests/types.test.ts +0 -92
- package/tsconfig.check.json +0 -17
- package/tsconfig.json +0 -19
- package/vitest.config.ts +0 -8
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
#!/usr/bin/env npx tsx
|
|
2
|
+
/**
|
|
3
|
+
* Supervisor Daemon — runs as a standalone background process.
|
|
4
|
+
*
|
|
5
|
+
* Survives Claude Code restarts. Manages session leases,
|
|
6
|
+
* detects stalls via OCR, and executes recovery actions.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* npx tsx scripts/supervisor-daemon.ts
|
|
10
|
+
* npx tsx scripts/supervisor-daemon.ts --poll 5000 --stall 300000
|
|
11
|
+
*
|
|
12
|
+
* State files:
|
|
13
|
+
* ~/.screenhand/supervisor/state.json — supervisor state
|
|
14
|
+
* ~/.screenhand/supervisor/recoveries.json — recovery queue
|
|
15
|
+
* ~/.screenhand/supervisor/supervisor.pid — PID of this process
|
|
16
|
+
* ~/.screenhand/supervisor/supervisor.log — log output
|
|
17
|
+
* ~/.screenhand/locks/ — session leases
|
|
18
|
+
*/
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import { BridgeClient } from "../src/native/bridge-client.js";
|
|
23
|
+
import { SessionSupervisor } from "../src/supervisor/supervisor.js";
|
|
24
|
+
// ── Config from CLI args ──
|
|
25
|
+
const args = process.argv.slice(2);
|
|
26
|
+
function getArg(name, fallback) {
|
|
27
|
+
const idx = args.indexOf("--" + name);
|
|
28
|
+
if (idx === -1)
|
|
29
|
+
return fallback;
|
|
30
|
+
return args[idx + 1] ?? fallback;
|
|
31
|
+
}
|
|
32
|
+
const POLL_MS = Number(getArg("poll", "5000"));
|
|
33
|
+
const STALL_MS = Number(getArg("stall", "300000"));
|
|
34
|
+
const LEASE_TIMEOUT_MS = Number(getArg("lease-timeout", "300000"));
|
|
35
|
+
const AUTO_RECOVER = getArg("no-auto-recover") === undefined;
|
|
36
|
+
const DRY_RUN = args.includes("--dry-run");
|
|
37
|
+
// ── Logging ──
|
|
38
|
+
const STATE_DIR = path.join(os.homedir(), ".screenhand", "supervisor");
|
|
39
|
+
fs.mkdirSync(STATE_DIR, { recursive: true });
|
|
40
|
+
const LOG_FILE = path.join(STATE_DIR, "supervisor.log");
|
|
41
|
+
const PID_FILE = path.join(STATE_DIR, "supervisor.pid");
|
|
42
|
+
const logStream = fs.createWriteStream(LOG_FILE, { flags: "a" });
|
|
43
|
+
let daemonized = false;
|
|
44
|
+
function log(msg) {
|
|
45
|
+
const line = `[${new Date().toISOString()}] ${msg}`;
|
|
46
|
+
logStream.write(line + "\n");
|
|
47
|
+
if (!daemonized)
|
|
48
|
+
process.stderr.write(line + "\n");
|
|
49
|
+
}
|
|
50
|
+
// ── Bridge setup ──
|
|
51
|
+
const scriptDir = import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname);
|
|
52
|
+
// When running from dist/scripts/, go up two levels to reach the real project root
|
|
53
|
+
const projectRoot = scriptDir.includes("/dist/")
|
|
54
|
+
? path.resolve(scriptDir, "../..")
|
|
55
|
+
: path.resolve(scriptDir, "..");
|
|
56
|
+
const bridgePath = process.platform === "win32"
|
|
57
|
+
? path.resolve(projectRoot, "native/windows-bridge/bin/Release/net8.0-windows/windows-bridge.exe")
|
|
58
|
+
: path.resolve(projectRoot, "native/macos-bridge/.build/release/macos-bridge");
|
|
59
|
+
const bridge = new BridgeClient(bridgePath);
|
|
60
|
+
let bridgeReady = false;
|
|
61
|
+
async function ensureBridge() {
|
|
62
|
+
if (!bridgeReady) {
|
|
63
|
+
await bridge.start();
|
|
64
|
+
bridgeReady = true;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// ── Supervisor ──
|
|
68
|
+
const supervisor = new SessionSupervisor({
|
|
69
|
+
pollMs: POLL_MS,
|
|
70
|
+
stallThresholdMs: STALL_MS,
|
|
71
|
+
leaseTimeoutMs: LEASE_TIMEOUT_MS,
|
|
72
|
+
autoRecover: AUTO_RECOVER,
|
|
73
|
+
});
|
|
74
|
+
let stopped = false;
|
|
75
|
+
// ── Recovery execution (the real control) ──
|
|
76
|
+
async function executeRecovery(recovery, lease) {
|
|
77
|
+
await ensureBridge();
|
|
78
|
+
switch (recovery.type) {
|
|
79
|
+
case "nudge": {
|
|
80
|
+
// Focus the app and press Enter to nudge a stalled agent
|
|
81
|
+
log(`Nudging session ${recovery.sessionId}: focusing ${lease.app}`);
|
|
82
|
+
try {
|
|
83
|
+
await bridge.call("app.focus", { bundleId: lease.app });
|
|
84
|
+
await sleep(300);
|
|
85
|
+
await bridge.call("cg.keyCombo", { keys: ["enter"] });
|
|
86
|
+
return "Focused app and pressed Enter";
|
|
87
|
+
}
|
|
88
|
+
catch (err) {
|
|
89
|
+
return `Nudge failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
case "restart": {
|
|
93
|
+
// Close and relaunch the app
|
|
94
|
+
log(`Restarting session ${recovery.sessionId}: relaunching ${lease.app}`);
|
|
95
|
+
try {
|
|
96
|
+
await bridge.call("app.focus", { bundleId: lease.app });
|
|
97
|
+
await sleep(200);
|
|
98
|
+
// Cmd+Q to quit, then relaunch
|
|
99
|
+
await bridge.call("cg.keyCombo", { keys: ["cmd", "q"] });
|
|
100
|
+
await sleep(2000);
|
|
101
|
+
await bridge.call("app.launch", { bundleId: lease.app });
|
|
102
|
+
return "App relaunched";
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
return `Restart failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
case "custom": {
|
|
109
|
+
// Type the custom instruction into the focused window
|
|
110
|
+
log(`Custom recovery for ${recovery.sessionId}: "${recovery.instruction.slice(0, 60)}"`);
|
|
111
|
+
try {
|
|
112
|
+
await bridge.call("app.focus", { bundleId: lease.app });
|
|
113
|
+
await sleep(300);
|
|
114
|
+
await bridge.call("cg.typeText", { text: recovery.instruction });
|
|
115
|
+
await sleep(100);
|
|
116
|
+
await bridge.call("cg.keyCombo", { keys: ["enter"] });
|
|
117
|
+
return `Typed: "${recovery.instruction.slice(0, 80)}"`;
|
|
118
|
+
}
|
|
119
|
+
catch (err) {
|
|
120
|
+
return `Custom recovery failed: ${err instanceof Error ? err.message : String(err)}`;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
case "escalate": {
|
|
124
|
+
// Notify via macOS notification
|
|
125
|
+
if (process.platform === "darwin") {
|
|
126
|
+
try {
|
|
127
|
+
const { execFileSync } = await import("node:child_process");
|
|
128
|
+
const msg = recovery.instruction.replace(/"/g, '\\"');
|
|
129
|
+
execFileSync("osascript", [
|
|
130
|
+
"-e",
|
|
131
|
+
`display notification "${msg}" with title "ScreenHand Supervisor" subtitle "Session needs help"`,
|
|
132
|
+
]);
|
|
133
|
+
return "Notification sent";
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
return "Escalation: notification failed, check supervisor log";
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return "Escalation logged — requires human intervention";
|
|
140
|
+
}
|
|
141
|
+
default:
|
|
142
|
+
return `Unknown recovery type: ${recovery.type}`;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
// ── OCR-based stall content capture ──
|
|
146
|
+
async function captureWindowContent(lease) {
|
|
147
|
+
try {
|
|
148
|
+
await ensureBridge();
|
|
149
|
+
const shot = await bridge.call("cg.captureWindow", {
|
|
150
|
+
windowId: lease.windowId,
|
|
151
|
+
});
|
|
152
|
+
const ocr = await bridge.call("vision.ocr", {
|
|
153
|
+
imagePath: shot.path,
|
|
154
|
+
});
|
|
155
|
+
return ocr.text;
|
|
156
|
+
}
|
|
157
|
+
catch {
|
|
158
|
+
// OCR failed — try AX tree as fallback
|
|
159
|
+
try {
|
|
160
|
+
const tree = await bridge.call("ax.getElementTree", {
|
|
161
|
+
pid: lease.client.pid ?? 0,
|
|
162
|
+
maxDepth: 4,
|
|
163
|
+
});
|
|
164
|
+
return extractText(tree);
|
|
165
|
+
}
|
|
166
|
+
catch {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function extractText(node, depth = 0) {
|
|
172
|
+
if (depth > 6)
|
|
173
|
+
return null;
|
|
174
|
+
if (node.value && typeof node.value === "string" && node.value.length > 20) {
|
|
175
|
+
return node.value;
|
|
176
|
+
}
|
|
177
|
+
if (node.children) {
|
|
178
|
+
for (const child of node.children) {
|
|
179
|
+
const found = extractText(child, depth + 1);
|
|
180
|
+
if (found)
|
|
181
|
+
return found;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
// ── Main poll loop ──
|
|
187
|
+
async function poll() {
|
|
188
|
+
// 1. Update screen content for all active sessions (for blocker matching)
|
|
189
|
+
const state = supervisor.getState();
|
|
190
|
+
for (const lease of state.sessions) {
|
|
191
|
+
const content = await captureWindowContent(lease);
|
|
192
|
+
if (content) {
|
|
193
|
+
supervisor.setScreenContent(lease.sessionId, content);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// 2. Let the supervisor run its poll cycle (detect stalls, create recoveries)
|
|
197
|
+
// The supervisor's internal pollCycle is on a timer, but we also call detectStalls
|
|
198
|
+
// to feed screen content into it
|
|
199
|
+
// 3. Execute pending recoveries with actual bridge actions
|
|
200
|
+
const pending = supervisor.getRecoveries("attempted");
|
|
201
|
+
for (const recovery of pending) {
|
|
202
|
+
const lease = state.sessions.find((s) => s.sessionId === recovery.sessionId);
|
|
203
|
+
if (!lease) {
|
|
204
|
+
supervisor.updateRecovery(recovery.id, "failed", "Session no longer active");
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
log(`Executing recovery ${recovery.id} (type=${recovery.type})`);
|
|
208
|
+
let resultText;
|
|
209
|
+
if (DRY_RUN) {
|
|
210
|
+
resultText = `[DRY RUN] Would execute ${recovery.type}: ${recovery.instruction.slice(0, 80)}`;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
resultText = await executeRecovery(recovery, lease);
|
|
214
|
+
}
|
|
215
|
+
const status = resultText.toLowerCase().includes("failed") ? "failed" : "succeeded";
|
|
216
|
+
supervisor.updateRecovery(recovery.id, status, resultText);
|
|
217
|
+
log(`Recovery ${recovery.id}: ${status} — ${resultText}`);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
async function main() {
|
|
221
|
+
// Enforce single daemon — abort if another is already running
|
|
222
|
+
const existingPid = supervisor.getExistingDaemonPid();
|
|
223
|
+
if (existingPid !== null && existingPid !== process.pid) {
|
|
224
|
+
const msg = `Another supervisor daemon is already running (pid=${existingPid}). Aborting.`;
|
|
225
|
+
log(msg);
|
|
226
|
+
process.stderr.write(msg + "\n");
|
|
227
|
+
process.exit(1);
|
|
228
|
+
}
|
|
229
|
+
fs.writeFileSync(PID_FILE, String(process.pid));
|
|
230
|
+
daemonized = true;
|
|
231
|
+
log(`Supervisor daemon started (pid=${process.pid})`);
|
|
232
|
+
log(`Config: poll=${POLL_MS}ms stall=${STALL_MS}ms lease-timeout=${LEASE_TIMEOUT_MS}ms auto-recover=${AUTO_RECOVER} dry-run=${DRY_RUN}`);
|
|
233
|
+
await supervisor.start();
|
|
234
|
+
log("Supervisor poll loop started");
|
|
235
|
+
// Additional poll for OCR + recovery execution
|
|
236
|
+
while (!stopped) {
|
|
237
|
+
try {
|
|
238
|
+
await poll();
|
|
239
|
+
}
|
|
240
|
+
catch (err) {
|
|
241
|
+
log(`Poll error: ${err instanceof Error ? err.message : String(err)}`);
|
|
242
|
+
}
|
|
243
|
+
await sleep(POLL_MS);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
function sleep(ms) {
|
|
247
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
248
|
+
}
|
|
249
|
+
// ── Graceful shutdown ──
|
|
250
|
+
process.on("SIGINT", shutdown);
|
|
251
|
+
process.on("SIGTERM", shutdown);
|
|
252
|
+
async function shutdown() {
|
|
253
|
+
if (stopped)
|
|
254
|
+
return;
|
|
255
|
+
stopped = true;
|
|
256
|
+
log("Shutting down...");
|
|
257
|
+
await supervisor.stop();
|
|
258
|
+
try {
|
|
259
|
+
fs.unlinkSync(PID_FILE);
|
|
260
|
+
}
|
|
261
|
+
catch { /* ignore */ }
|
|
262
|
+
try {
|
|
263
|
+
await bridge.stop();
|
|
264
|
+
}
|
|
265
|
+
catch { /* ignore */ }
|
|
266
|
+
logStream.end();
|
|
267
|
+
process.exit(0);
|
|
268
|
+
}
|
|
269
|
+
main().catch((err) => {
|
|
270
|
+
log(`Fatal: ${err instanceof Error ? err.message : String(err)}`);
|
|
271
|
+
process.exit(1);
|
|
272
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
#!/usr/bin/env npx tsx
|
|
2
|
+
/**
|
|
3
|
+
* Worker Daemon — runs as a standalone background process.
|
|
4
|
+
*
|
|
5
|
+
* Survives MCP/client restarts. Continuously processes the job queue
|
|
6
|
+
* via JobRunner with playbook engine support.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* npx tsx scripts/worker-daemon.ts
|
|
10
|
+
* npx tsx scripts/worker-daemon.ts --poll 3000 --max-jobs 0
|
|
11
|
+
*
|
|
12
|
+
* State files:
|
|
13
|
+
* ~/.screenhand/worker/state.json — worker status + recent results
|
|
14
|
+
* ~/.screenhand/worker/worker.pid — PID of this process
|
|
15
|
+
* ~/.screenhand/worker/worker.log — log output
|
|
16
|
+
* ~/.screenhand/jobs/ — job persistence
|
|
17
|
+
* ~/.screenhand/locks/ — session leases
|
|
18
|
+
*/
|
|
19
|
+
import path from "node:path";
|
|
20
|
+
import fs from "node:fs";
|
|
21
|
+
import os from "node:os";
|
|
22
|
+
import { BridgeClient } from "../src/native/bridge-client.js";
|
|
23
|
+
import { SessionSupervisor, LeaseManager } from "../src/supervisor/supervisor.js";
|
|
24
|
+
import { JobManager } from "../src/jobs/manager.js";
|
|
25
|
+
import { JobRunner } from "../src/jobs/runner.js";
|
|
26
|
+
import { PlaybookEngine } from "../src/playbook/engine.js";
|
|
27
|
+
import { PlaybookStore } from "../src/playbook/store.js";
|
|
28
|
+
import { AccessibilityAdapter } from "../src/runtime/accessibility-adapter.js";
|
|
29
|
+
import { AutomationRuntimeService } from "../src/runtime/service.js";
|
|
30
|
+
import { TimelineLogger } from "../src/logging/timeline-logger.js";
|
|
31
|
+
import { MemoryService } from "../src/memory/service.js";
|
|
32
|
+
import { WORKER_DIR, WORKER_PID_FILE, WORKER_LOG_FILE, getWorkerDaemonPid, writeWorkerStatus, } from "../src/jobs/worker.js";
|
|
33
|
+
// ── Config from CLI args ──
|
|
34
|
+
const args = process.argv.slice(2);
|
|
35
|
+
function getArg(name, fallback) {
|
|
36
|
+
const idx = args.indexOf("--" + name);
|
|
37
|
+
if (idx === -1)
|
|
38
|
+
return fallback;
|
|
39
|
+
return args[idx + 1] ?? fallback;
|
|
40
|
+
}
|
|
41
|
+
const POLL_MS = Number(getArg("poll", "3000"));
|
|
42
|
+
const MAX_JOBS = Number(getArg("max-jobs", "0")); // 0 = unlimited
|
|
43
|
+
// ── Directories ──
|
|
44
|
+
const JOB_DIR = path.join(os.homedir(), ".screenhand", "jobs");
|
|
45
|
+
const LOCK_DIR = path.join(os.homedir(), ".screenhand", "locks");
|
|
46
|
+
const PLAYBOOKS_DIR = path.join(os.homedir(), ".screenhand", "playbooks");
|
|
47
|
+
const SUPERVISOR_STATE_DIR = path.join(os.homedir(), ".screenhand", "supervisor");
|
|
48
|
+
fs.mkdirSync(WORKER_DIR, { recursive: true });
|
|
49
|
+
fs.mkdirSync(JOB_DIR, { recursive: true });
|
|
50
|
+
// ── Logging ──
|
|
51
|
+
const logStream = fs.createWriteStream(WORKER_LOG_FILE, { flags: "a" });
|
|
52
|
+
let daemonized = false;
|
|
53
|
+
function log(msg) {
|
|
54
|
+
const line = `[${new Date().toISOString()}] ${msg}`;
|
|
55
|
+
logStream.write(line + "\n");
|
|
56
|
+
if (!daemonized)
|
|
57
|
+
process.stderr.write(line + "\n");
|
|
58
|
+
}
|
|
59
|
+
// ── Bridge setup ──
|
|
60
|
+
const scriptDir = import.meta.dirname ?? path.dirname(new URL(import.meta.url).pathname);
|
|
61
|
+
const projectRoot = scriptDir.includes("/dist/")
|
|
62
|
+
? path.resolve(scriptDir, "../..")
|
|
63
|
+
: path.resolve(scriptDir, "..");
|
|
64
|
+
const bridgePath = process.platform === "win32"
|
|
65
|
+
? path.resolve(projectRoot, "native/windows-bridge/bin/Release/net8.0-windows/windows-bridge.exe")
|
|
66
|
+
: path.resolve(projectRoot, "native/macos-bridge/.build/release/macos-bridge");
|
|
67
|
+
const bridge = new BridgeClient(bridgePath);
|
|
68
|
+
let bridgeReady = false;
|
|
69
|
+
async function ensureBridge() {
|
|
70
|
+
if (!bridgeReady) {
|
|
71
|
+
await bridge.start();
|
|
72
|
+
bridgeReady = true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// ── Services ──
|
|
76
|
+
const leaseManager = new LeaseManager(LOCK_DIR);
|
|
77
|
+
const supervisor = new SessionSupervisor({
|
|
78
|
+
stateDir: SUPERVISOR_STATE_DIR,
|
|
79
|
+
lockDir: LOCK_DIR,
|
|
80
|
+
});
|
|
81
|
+
const memory = new MemoryService(os.homedir());
|
|
82
|
+
const jobManager = new JobManager({ jobDir: JOB_DIR, memory, supervisor });
|
|
83
|
+
jobManager.init();
|
|
84
|
+
// ── State ──
|
|
85
|
+
let stopped = false;
|
|
86
|
+
let processing = false;
|
|
87
|
+
const recentResults = [];
|
|
88
|
+
const MAX_RECENT = 50;
|
|
89
|
+
let jobsProcessed = 0;
|
|
90
|
+
let jobsDone = 0;
|
|
91
|
+
let jobsFailed = 0;
|
|
92
|
+
let jobsBlocked = 0;
|
|
93
|
+
let lastJobId = null;
|
|
94
|
+
let lastJobState = null;
|
|
95
|
+
let startedAt = null;
|
|
96
|
+
function buildStatus() {
|
|
97
|
+
return {
|
|
98
|
+
pid: process.pid,
|
|
99
|
+
running: !stopped,
|
|
100
|
+
startedAt,
|
|
101
|
+
pollMs: POLL_MS,
|
|
102
|
+
maxJobs: MAX_JOBS,
|
|
103
|
+
jobsProcessed,
|
|
104
|
+
jobsDone,
|
|
105
|
+
jobsFailed,
|
|
106
|
+
jobsBlocked,
|
|
107
|
+
lastJobId,
|
|
108
|
+
lastJobState,
|
|
109
|
+
uptimeMs: startedAt ? Date.now() - new Date(startedAt).getTime() : 0,
|
|
110
|
+
recentResults: recentResults.slice(-MAX_RECENT),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
function persistState() {
|
|
114
|
+
try {
|
|
115
|
+
writeWorkerStatus(buildStatus());
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// Non-fatal
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
function recordResult(result) {
|
|
122
|
+
jobsProcessed++;
|
|
123
|
+
lastJobId = result.jobId;
|
|
124
|
+
lastJobState = result.finalState;
|
|
125
|
+
switch (result.finalState) {
|
|
126
|
+
case "done":
|
|
127
|
+
jobsDone++;
|
|
128
|
+
break;
|
|
129
|
+
case "failed":
|
|
130
|
+
jobsFailed++;
|
|
131
|
+
break;
|
|
132
|
+
case "blocked":
|
|
133
|
+
case "waiting_human":
|
|
134
|
+
jobsBlocked++;
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
recentResults.push(result);
|
|
138
|
+
if (recentResults.length > MAX_RECENT)
|
|
139
|
+
recentResults.shift();
|
|
140
|
+
log(`Completed: ${result.jobId} → ${result.finalState} (${result.stepsCompleted}/${result.totalSteps} steps, ${result.durationMs}ms)`);
|
|
141
|
+
persistState();
|
|
142
|
+
}
|
|
143
|
+
// ── Main loop ──
|
|
144
|
+
async function main() {
|
|
145
|
+
// Enforce single daemon
|
|
146
|
+
const existingPid = getWorkerDaemonPid();
|
|
147
|
+
if (existingPid !== null && existingPid !== process.pid) {
|
|
148
|
+
const msg = `Another worker daemon is already running (pid=${existingPid}). Aborting.`;
|
|
149
|
+
log(msg);
|
|
150
|
+
process.stderr.write(msg + "\n");
|
|
151
|
+
process.exit(1);
|
|
152
|
+
}
|
|
153
|
+
fs.writeFileSync(WORKER_PID_FILE, String(process.pid));
|
|
154
|
+
daemonized = true;
|
|
155
|
+
startedAt = new Date().toISOString();
|
|
156
|
+
log(`Worker daemon started (pid=${process.pid})`);
|
|
157
|
+
log(`Config: poll=${POLL_MS}ms max-jobs=${MAX_JOBS || "unlimited"}`);
|
|
158
|
+
// Ensure bridge is ready
|
|
159
|
+
await ensureBridge();
|
|
160
|
+
// Build playbook engine stack
|
|
161
|
+
const adapter = new AccessibilityAdapter(bridge);
|
|
162
|
+
const logger = new TimelineLogger();
|
|
163
|
+
const runtimeService = new AutomationRuntimeService(adapter, logger);
|
|
164
|
+
const playbookEngine = new PlaybookEngine(runtimeService);
|
|
165
|
+
const playbookStore = new PlaybookStore(PLAYBOOKS_DIR);
|
|
166
|
+
playbookStore.load();
|
|
167
|
+
const runner = new JobRunner(bridge, jobManager, leaseManager, supervisor, {
|
|
168
|
+
playbookEngine,
|
|
169
|
+
playbookStore,
|
|
170
|
+
runtimeService,
|
|
171
|
+
onLog: log,
|
|
172
|
+
});
|
|
173
|
+
persistState();
|
|
174
|
+
// Poll loop
|
|
175
|
+
while (!stopped) {
|
|
176
|
+
if (!processing) {
|
|
177
|
+
processing = true;
|
|
178
|
+
try {
|
|
179
|
+
const result = await runner.run();
|
|
180
|
+
if (result) {
|
|
181
|
+
recordResult(result);
|
|
182
|
+
// Check maxJobs limit
|
|
183
|
+
if (MAX_JOBS > 0 && jobsProcessed >= MAX_JOBS) {
|
|
184
|
+
log(`Reached max-jobs limit (${MAX_JOBS})`);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
// Job found — poll again immediately
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
catch (err) {
|
|
192
|
+
log(`Poll error: ${err instanceof Error ? err.message : String(err)}`);
|
|
193
|
+
}
|
|
194
|
+
finally {
|
|
195
|
+
processing = false;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
// Queue empty or error — wait before next poll
|
|
199
|
+
await sleep(POLL_MS);
|
|
200
|
+
}
|
|
201
|
+
log(`Worker daemon exiting (${jobsProcessed} jobs: ${jobsDone} done, ${jobsFailed} failed, ${jobsBlocked} blocked)`);
|
|
202
|
+
await shutdown();
|
|
203
|
+
}
|
|
204
|
+
function sleep(ms) {
|
|
205
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
206
|
+
}
|
|
207
|
+
// ── Graceful shutdown ──
|
|
208
|
+
process.on("SIGINT", () => { stopped = true; });
|
|
209
|
+
process.on("SIGTERM", () => { stopped = true; });
|
|
210
|
+
async function shutdown() {
|
|
211
|
+
stopped = true;
|
|
212
|
+
persistState();
|
|
213
|
+
try {
|
|
214
|
+
fs.unlinkSync(WORKER_PID_FILE);
|
|
215
|
+
}
|
|
216
|
+
catch { /* ignore */ }
|
|
217
|
+
try {
|
|
218
|
+
await bridge.stop();
|
|
219
|
+
}
|
|
220
|
+
catch { /* ignore */ }
|
|
221
|
+
logStream.end();
|
|
222
|
+
process.exit(0);
|
|
223
|
+
}
|
|
224
|
+
main().catch((err) => {
|
|
225
|
+
log(`Fatal: ${err instanceof Error ? err.message : String(err)}`);
|
|
226
|
+
persistState();
|
|
227
|
+
process.exit(1);
|
|
228
|
+
});
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright (C) 2025 Clazro Technology Private Limited
|
|
3
|
+
// SPDX-License-Identifier: AGPL-3.0-only
|
|
4
|
+
//
|
|
5
|
+
// This file is part of ScreenHand.
|
|
6
|
+
//
|
|
7
|
+
// ScreenHand is free software: you can redistribute it and/or modify
|
|
8
|
+
// it under the terms of the GNU Affero General Public License as
|
|
9
|
+
// published by the Free Software Foundation, version 3.
|
|
10
|
+
//
|
|
11
|
+
// ScreenHand is distributed in the hope that it will be useful,
|
|
12
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14
|
+
// GNU Affero General Public License for more details.
|
|
15
|
+
//
|
|
16
|
+
// You should have received a copy of the GNU Affero General Public License
|
|
17
|
+
// along with ScreenHand. If not, see <https://www.gnu.org/licenses/>.
|
|
18
|
+
import { TimelineLogger } from "../logging/timeline-logger.js";
|
|
19
|
+
import { AutomationRuntimeService } from "../runtime/service.js";
|
|
20
|
+
import { runAgentLoop } from "./loop.js";
|
|
21
|
+
const task = process.argv.slice(2).join(" ");
|
|
22
|
+
if (!task) {
|
|
23
|
+
console.error("Usage: screenhand-agent <task description>");
|
|
24
|
+
console.error("Example: screenhand-agent \"Open Safari and search for MCP protocol\"");
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
async function createAdapter() {
|
|
28
|
+
const adapterType = process.env.SCREENHAND_ADAPTER ?? "accessibility";
|
|
29
|
+
switch (adapterType) {
|
|
30
|
+
case "placeholder": {
|
|
31
|
+
const { PlaceholderAppAdapter } = await import("../runtime/app-adapter.js");
|
|
32
|
+
return new PlaceholderAppAdapter();
|
|
33
|
+
}
|
|
34
|
+
case "cdp": {
|
|
35
|
+
const { CdpChromeAdapter } = await import("../runtime/cdp-chrome-adapter.js");
|
|
36
|
+
return new CdpChromeAdapter({ headless: process.env.SCREENHAND_HEADLESS === "1" });
|
|
37
|
+
}
|
|
38
|
+
case "composite": {
|
|
39
|
+
const { BridgeClient } = await import("../native/bridge-client.js");
|
|
40
|
+
const { CompositeAdapter } = await import("../runtime/composite-adapter.js");
|
|
41
|
+
return new CompositeAdapter(new BridgeClient(), { headless: process.env.SCREENHAND_HEADLESS === "1" });
|
|
42
|
+
}
|
|
43
|
+
default: {
|
|
44
|
+
const { BridgeClient } = await import("../native/bridge-client.js");
|
|
45
|
+
const { AccessibilityAdapter } = await import("../runtime/accessibility-adapter.js");
|
|
46
|
+
return new AccessibilityAdapter(new BridgeClient());
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
try {
|
|
51
|
+
const adapter = await createAdapter();
|
|
52
|
+
const runtime = new AutomationRuntimeService(adapter, new TimelineLogger());
|
|
53
|
+
const session = await runtime.sessionStart();
|
|
54
|
+
console.log(`\n🔄 Task: ${task}`);
|
|
55
|
+
console.log(` Session: ${session.sessionId}`);
|
|
56
|
+
console.log(` Model: ${process.env.SCREENHAND_MODEL ?? "claude-sonnet-4-20250514"}\n`);
|
|
57
|
+
const cliModel = process.env.SCREENHAND_MODEL;
|
|
58
|
+
const result = await runAgentLoop(runtime, session.sessionId, task, {
|
|
59
|
+
maxSteps: parseInt(process.env.SCREENHAND_MAX_STEPS ?? "50", 10),
|
|
60
|
+
...(cliModel ? { model: cliModel } : {}),
|
|
61
|
+
onStep: (step) => {
|
|
62
|
+
const icon = step.done ? "✅" : step.action ? "→" : "⚠️";
|
|
63
|
+
console.log(` ${icon} [${step.index}] ${step.reasoning.slice(0, 100)}`);
|
|
64
|
+
if (step.action && step.action.tool !== "done") {
|
|
65
|
+
console.log(` ${step.action.tool}: ${JSON.stringify(step.action).slice(0, 120)}`);
|
|
66
|
+
}
|
|
67
|
+
if (step.result) {
|
|
68
|
+
console.log(` Result: ${step.result.slice(0, 100)}`);
|
|
69
|
+
}
|
|
70
|
+
console.log(` (${step.durationMs}ms)\n`);
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
console.log(`\n${"=".repeat(60)}`);
|
|
74
|
+
console.log(`${result.success ? "✅ SUCCESS" : "❌ INCOMPLETE"}: ${result.summary}`);
|
|
75
|
+
console.log(`Steps: ${result.steps.length} | Total: ${result.totalMs}ms`);
|
|
76
|
+
console.log(`${"=".repeat(60)}\n`);
|
|
77
|
+
process.exit(result.success ? 0 : 1);
|
|
78
|
+
}
|
|
79
|
+
catch (e) {
|
|
80
|
+
console.error(`Fatal: ${e instanceof Error ? e.message : String(e)}`);
|
|
81
|
+
process.exit(1);
|
|
82
|
+
}
|