@igorjpimenta/winch-opencode 2026.11.24-beta.1 → 2026.11.25-beta.2

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 (2) hide show
  1. package/index.js +36 -6
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -29,7 +29,7 @@ function processCommand(pid) {
29
29
  }
30
30
  }
31
31
 
32
- export function createWakeRunner({ logger = console, stateDir = wakeStateDir() } = {}) {
32
+ export function createWakeRunner({ logger = console, stateDir = wakeStateDir(), dispatchUrl = null, bearer = null, consumerId = null } = {}) {
33
33
  // Detached children lead their own process group; kill the group so a
34
34
  // reaped turn's tool and MCP subprocesses go with it.
35
35
  const reap = (pid) => {
@@ -39,7 +39,7 @@ export function createWakeRunner({ logger = console, stateDir = wakeStateDir() }
39
39
 
40
40
  const recordPath = (ownerPid, childPid) => path.join(stateDir, `wake-${ownerPid}-${childPid}.json`);
41
41
 
42
- const wake = ({ agent, message }) =>
42
+ const spawnWake = ({ agent, message }) =>
43
43
  new Promise((resolve, reject) => {
44
44
  const bin = process.env.OPENCODE_BIN || "opencode";
45
45
  const child = spawn(bin, ["run", "--agent", agent, message], {
@@ -74,6 +74,35 @@ export function createWakeRunner({ logger = console, stateDir = wakeStateDir() }
74
74
  child.unref();
75
75
  });
76
76
 
77
+ // Core owns the wake window: it counts delivered-but-unsettled children and
78
+ // refuses to exceed WINCH_DISPATCH_WAKE_MAX. This map is the fail-safe for
79
+ // the pump/recover race — one live child per key, never a queue.
80
+ const inflight = new Set();
81
+
82
+ const settleUrl = dispatchUrl ? `${String(dispatchUrl).replace(/\/+$/, "")}/dispatch/notify` : null;
83
+
84
+ // The window frees only when the child reports its exit.
85
+ const settle = (admissionId) => {
86
+ if (!admissionId || !settleUrl) return;
87
+ const headers = { "content-type": "application/json" };
88
+ if (bearer) headers.authorization = `Bearer ${bearer}`;
89
+ void fetch(settleUrl, {
90
+ method: "POST",
91
+ headers,
92
+ body: JSON.stringify({ type: "settle", admissionId, consumerId }),
93
+ signal: AbortSignal.timeout(3000),
94
+ }).catch((err) => logger?.warn?.(`winch: settle ${admissionId} failed: ${err?.message ?? err}`));
95
+ };
96
+
97
+ const wake = ({ agent, message, key = null, admissionId = null }) => {
98
+ if (key && inflight.has(key)) return Promise.resolve({ deduped: true });
99
+ if (key) inflight.add(key);
100
+ return spawnWake({ agent, message }).finally(() => {
101
+ if (key) inflight.delete(key);
102
+ settle(admissionId);
103
+ });
104
+ };
105
+
77
106
  // Kill wake children whose spawning owner is no longer alive. Called at
78
107
  // dispatch startup and on a sweep timer; never on close, because a child
79
108
  // still owned by a live instance is not an orphan.
@@ -112,18 +141,19 @@ function consumerIdFor(harness, cfg = {}) {
112
141
  }
113
142
 
114
143
  function start({ cfg = {}, harness, logger }) {
115
- const runner = createWakeRunner({ logger, stateDir: cfg.wakeStateDir });
116
- runner.reapOrphans();
117
-
118
144
  const executorId = cfg.winchExecutorId ?? process.env.WINCH_EXECUTOR_ID;
119
145
  const consumerId = consumerIdFor(harness, cfg);
120
146
  const bearer = process.env.WINCH_WEBHOOK_BEARER;
147
+ const dispatchUrl = process.env.WINCH_DISPATCH_URL ?? DEFAULT_DISPATCH_URL;
148
+ const runner = createWakeRunner({ logger, stateDir: cfg.wakeStateDir, dispatchUrl, bearer, consumerId });
149
+ runner.reapOrphans();
150
+
121
151
  if (!executorId || !bearer) {
122
152
  logger?.warn?.("winch: dispatch needs WINCH_EXECUTOR_ID and WINCH_WEBHOOK_BEARER; receiver disabled");
123
153
  return null;
124
154
  }
125
155
  const dispatch = startDispatch({
126
- baseUrl: process.env.WINCH_DISPATCH_URL ?? DEFAULT_DISPATCH_URL,
156
+ baseUrl: dispatchUrl,
127
157
  harness,
128
158
  consumerId,
129
159
  executorId,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@igorjpimenta/winch-opencode",
3
- "version": "2026.11.24-beta.1",
3
+ "version": "2026.11.25-beta.2",
4
4
  "description": "OpenCode companion for winch: stamps agent and session identity on winch tool calls, receives HTTP dispatch wakes, reports LLM usage, and reconciles assigned in-progress work. No SDK dependency \u2014 a plain id/setup/server export.",
5
5
  "license": "MIT",
6
6
  "type": "module",