@igorjpimenta/winch 2026.11.22-beta.2 → 2026.11.22-beta.3
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 +1 -1
- package/plugins/README.md +2 -2
- package/plugins/opencode/winch/index.js +111 -17
package/package.json
CHANGED
package/plugins/README.md
CHANGED
|
@@ -205,7 +205,7 @@ so nobody but the adapter can write the identity args.
|
|
|
205
205
|
|
|
206
206
|
### Remote (HTTP) — loopback with a bearer
|
|
207
207
|
|
|
208
|
-
A persistent server (`winch serve --mcp http --port
|
|
208
|
+
A persistent server (`winch serve --mcp http --port 38292`) avoids per-call
|
|
209
209
|
spawns. Read the bearer from a `0600` file, not from the environment:
|
|
210
210
|
|
|
211
211
|
```jsonc
|
|
@@ -213,7 +213,7 @@ spawns. Read the bearer from a `0600` file, not from the environment:
|
|
|
213
213
|
"mcp": {
|
|
214
214
|
"winch": {
|
|
215
215
|
"type": "remote",
|
|
216
|
-
"url": "http://127.0.0.1:
|
|
216
|
+
"url": "http://127.0.0.1:38292/mcp",
|
|
217
217
|
"headers": {
|
|
218
218
|
"Authorization": "Bearer {file:~/.secrets/winch-bearer}",
|
|
219
219
|
"X-Winch-Harness": "opencode"
|
|
@@ -1,24 +1,109 @@
|
|
|
1
1
|
import { createWinchHooks } from "./hooks.js";
|
|
2
2
|
import { stampWinchEvent } from "./identity.js";
|
|
3
3
|
import { startDispatch, DEFAULT_DISPATCH_URL } from "./dispatch.js";
|
|
4
|
-
import { spawn } from "node:child_process";
|
|
5
|
-
import {
|
|
4
|
+
import { spawn, execFileSync } from "node:child_process";
|
|
5
|
+
import { mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
|
|
6
|
+
import os, { hostname } from "node:os";
|
|
7
|
+
import path from "node:path";
|
|
6
8
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
// A wake child is orphaned only when the process that spawned it is gone. A
|
|
10
|
+
// child whose owner is still running is doing real work, so it must not be
|
|
11
|
+
// reaped. The sweep therefore kills nothing except leftovers from a dead owner.
|
|
12
|
+
const SWEEP_MS = 60 * 1000;
|
|
13
|
+
|
|
14
|
+
function wakeStateDir() {
|
|
15
|
+
return path.join(os.homedir(), ".opencode", "state", "winch-wakes");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function pidAlive(pid) {
|
|
19
|
+
try { process.kill(pid, 0); return true; }
|
|
20
|
+
catch (err) { return err?.code === "EPERM"; }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function processCommand(pid) {
|
|
24
|
+
try {
|
|
25
|
+
return execFileSync("ps", ["-o", "command=", "-p", String(pid)], { encoding: "utf8" }).trim();
|
|
26
|
+
} catch {
|
|
27
|
+
return "";
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function createWakeRunner({ logger = console, stateDir = wakeStateDir() } = {}) {
|
|
32
|
+
// Detached children lead their own process group; kill the group so a
|
|
33
|
+
// reaped turn's tool and MCP subprocesses go with it.
|
|
34
|
+
const reap = (pid) => {
|
|
35
|
+
try { process.kill(-pid, "SIGKILL"); }
|
|
36
|
+
catch { try { process.kill(pid, "SIGKILL"); } catch {} }
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const recordPath = (ownerPid, childPid) => path.join(stateDir, `wake-${ownerPid}-${childPid}.json`);
|
|
40
|
+
|
|
41
|
+
const wake = ({ agent, message }) =>
|
|
42
|
+
new Promise((resolve, reject) => {
|
|
43
|
+
const bin = process.env.OPENCODE_BIN || "opencode";
|
|
44
|
+
const child = spawn(bin, ["run", "--agent", agent, message], {
|
|
45
|
+
detached: true,
|
|
46
|
+
stdio: ["ignore", "ignore", "pipe"],
|
|
47
|
+
// A wake child loads the same config and would start its own dispatch
|
|
48
|
+
// listener, registering as another executor; then every wake fans out
|
|
49
|
+
// into more waking agents. Keep dispatch to the root instance only.
|
|
50
|
+
env: { ...process.env, WINCH_DISPATCH_ENABLED: "0" },
|
|
51
|
+
});
|
|
52
|
+
const file = child.pid ? recordPath(process.pid, child.pid) : null;
|
|
53
|
+
if (file) {
|
|
54
|
+
try {
|
|
55
|
+
mkdirSync(stateDir, { recursive: true });
|
|
56
|
+
writeFileSync(file, JSON.stringify({ pid: child.pid, ownerPid: process.pid, agent, message }));
|
|
57
|
+
} catch (err) {
|
|
58
|
+
logger?.warn?.(`winch: wake record not written: ${err?.message ?? err}`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const forget = () => { try { if (file) unlinkSync(file); } catch {} };
|
|
62
|
+
let stderr = "";
|
|
63
|
+
child.stderr?.on("data", (d) => { stderr = (stderr + d.toString()).slice(-2000); });
|
|
64
|
+
child.on("error", (err) => { forget(); reject(err); });
|
|
65
|
+
child.on("exit", (code) => {
|
|
66
|
+
forget();
|
|
67
|
+
code === 0 ? resolve() : reject(new Error(`exit ${code}${stderr ? `: ${stderr.trim().split("\n").slice(-2).join(" | ")}` : ""}`));
|
|
68
|
+
});
|
|
69
|
+
// Unref: an agent run must not hold its caller's event loop open, or a
|
|
70
|
+
// daemon could not shut down while a wake is in flight. A caller that
|
|
71
|
+
// awaits wake() must therefore keep its own handle alive — the exit
|
|
72
|
+
// event will not dispatch on an otherwise empty loop.
|
|
73
|
+
child.unref();
|
|
13
74
|
});
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
75
|
+
|
|
76
|
+
// Kill wake children whose spawning owner is no longer alive. Called at
|
|
77
|
+
// dispatch startup and on a sweep timer; never on close, because a child
|
|
78
|
+
// still owned by a live instance is not an orphan.
|
|
79
|
+
function reapOrphans() {
|
|
80
|
+
let names = [];
|
|
81
|
+
try { names = readdirSync(stateDir); } catch { return 0; }
|
|
82
|
+
let killed = 0;
|
|
83
|
+
for (const name of names) {
|
|
84
|
+
if (!name.startsWith("wake-") || !name.endsWith(".json")) continue;
|
|
85
|
+
const file = path.join(stateDir, name);
|
|
86
|
+
let entry;
|
|
87
|
+
try { entry = JSON.parse(readFileSync(file, "utf8")); }
|
|
88
|
+
catch { try { unlinkSync(file); } catch {} continue; }
|
|
89
|
+
if (!entry?.pid || !entry?.ownerPid) { try { unlinkSync(file); } catch {} continue; }
|
|
90
|
+
if (pidAlive(entry.ownerPid)) continue; // owner alive: not an orphan
|
|
91
|
+
if (pidAlive(entry.pid)) {
|
|
92
|
+
// Guard PID reuse: only kill a process still running this wake's
|
|
93
|
+
// command, never whatever now occupies the recycled pid.
|
|
94
|
+
const cmd = processCommand(entry.pid);
|
|
95
|
+
if (cmd.includes("--agent") && entry.message && cmd.includes(entry.message)) {
|
|
96
|
+
reap(entry.pid);
|
|
97
|
+
killed += 1;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
try { unlinkSync(file); } catch {}
|
|
101
|
+
}
|
|
102
|
+
if (killed) logger?.warn?.(`winch: reaped ${killed} orphaned wake(s)`);
|
|
103
|
+
return killed;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return { wake, reapOrphans };
|
|
22
107
|
}
|
|
23
108
|
|
|
24
109
|
function consumerIdFor(harness, cfg = {}) {
|
|
@@ -26,6 +111,9 @@ function consumerIdFor(harness, cfg = {}) {
|
|
|
26
111
|
}
|
|
27
112
|
|
|
28
113
|
function start({ cfg = {}, harness, logger }) {
|
|
114
|
+
const runner = createWakeRunner({ logger, stateDir: cfg.wakeStateDir });
|
|
115
|
+
runner.reapOrphans();
|
|
116
|
+
|
|
29
117
|
const executorId = cfg.winchExecutorId ?? process.env.WINCH_EXECUTOR_ID;
|
|
30
118
|
const consumerId = consumerIdFor(harness, cfg);
|
|
31
119
|
const bearer = process.env.WINCH_WEBHOOK_BEARER;
|
|
@@ -39,12 +127,18 @@ function start({ cfg = {}, harness, logger }) {
|
|
|
39
127
|
consumerId,
|
|
40
128
|
executorId,
|
|
41
129
|
bearer,
|
|
42
|
-
wake:
|
|
130
|
+
wake: runner.wake,
|
|
43
131
|
port: Number(cfg.dispatchPort ?? process.env.WINCH_DISPATCH_PORT ?? 0),
|
|
44
132
|
reconcileMs: Number(cfg.dispatchReconcileMs ?? process.env.WINCH_DISPATCH_RECONCILE_MS ?? 30000),
|
|
45
133
|
logger,
|
|
46
134
|
});
|
|
47
135
|
dispatch.ready.catch((err) => logger?.warn?.(`winch: dispatch listener failed: ${err?.message ?? err}`));
|
|
136
|
+
|
|
137
|
+
const sweep = setInterval(() => runner.reapOrphans(), Number(cfg.wakeSweepMs ?? SWEEP_MS));
|
|
138
|
+
sweep.unref?.();
|
|
139
|
+
const close = dispatch.close.bind(dispatch);
|
|
140
|
+
dispatch.close = async () => { clearInterval(sweep); await close(); };
|
|
141
|
+
dispatch.reapOrphans = runner.reapOrphans;
|
|
48
142
|
return dispatch;
|
|
49
143
|
}
|
|
50
144
|
|