@shumkov/orchestra 0.4.3 → 0.5.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/lib/process/cli-process.js +69 -10
- package/lib/process/factory.js +5 -0
- package/lib/tmux/tmux-runner.js +31 -10
- package/package.json +1 -1
|
@@ -117,6 +117,26 @@ const DEFAULT_TOOL_RATE_LIMIT_PER_SEC = 5; // P2 ADV-6: cap on reply tool call
|
|
|
117
117
|
const DEFAULT_TOOL_RATE_BURST = 20; // ADV-6: token bucket capacity
|
|
118
118
|
const DEFAULT_QUEUE_CAP = 50; // Parity P2: match SDK/tmux pendingQueue cap
|
|
119
119
|
|
|
120
|
+
function validateSessionLauncher(sessionLauncher) {
|
|
121
|
+
if (!sessionLauncher) return null;
|
|
122
|
+
if (typeof sessionLauncher !== 'string' || !path.isAbsolute(sessionLauncher)) {
|
|
123
|
+
throw Object.assign(new TypeError('CliProcess: sessionLauncher must be an absolute executable path'), {
|
|
124
|
+
code: 'SESSION_LAUNCHER_INVALID',
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
try {
|
|
128
|
+
if (!fs.statSync(sessionLauncher).isFile()) {
|
|
129
|
+
throw new Error('not a regular file');
|
|
130
|
+
}
|
|
131
|
+
fs.accessSync(sessionLauncher, fs.constants.X_OK);
|
|
132
|
+
} catch {
|
|
133
|
+
throw Object.assign(new TypeError(`CliProcess: sessionLauncher is not an executable file: ${sessionLauncher}`), {
|
|
134
|
+
code: 'SESSION_LAUNCHER_INVALID',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return sessionLauncher;
|
|
138
|
+
}
|
|
139
|
+
|
|
120
140
|
// Review F#17: mid-turn dialog watchdog. Even though channels uses MCP for IO,
|
|
121
141
|
// the underlying claude TUI can still pop interactive prompts mid-turn that
|
|
122
142
|
// don't surface as MCP notifications (session-age, future "approaching usage
|
|
@@ -253,6 +273,7 @@ class CliProcess extends Process {
|
|
|
253
273
|
sessionKey, chatId, threadId, label,
|
|
254
274
|
tmuxRunner, botName,
|
|
255
275
|
claudeBin = null,
|
|
276
|
+
sessionLauncher = process.env.ORCHESTRA_SESSION_LAUNCHER || null,
|
|
256
277
|
toolDispatcher,
|
|
257
278
|
// Extraction-time API (orchestra): the consumer injects its surface-rendering
|
|
258
279
|
// hint (appended to the system prompt) and outbound file-size cap, so the engine
|
|
@@ -308,6 +329,7 @@ class CliProcess extends Process {
|
|
|
308
329
|
throw new TypeError('CliProcess: claudeBin required (or ORCHESTRA_CLAUDE_BIN env)');
|
|
309
330
|
}
|
|
310
331
|
this.claudeBin = claudeBin || process.env.ORCHESTRA_CLAUDE_BIN;
|
|
332
|
+
this.sessionLauncher = validateSessionLauncher(sessionLauncher);
|
|
311
333
|
this.toolDispatcher = toolDispatcher;
|
|
312
334
|
this.displayHint = displayHint;
|
|
313
335
|
this._injectedMaxOutboundFileBytes = maxOutboundFileBytes;
|
|
@@ -345,6 +367,7 @@ class CliProcess extends Process {
|
|
|
345
367
|
this.mcpConfigPath = null; // P0 #1: 0o600 tmp file holding bridge env (no argv leak)
|
|
346
368
|
this.tmuxSession = null; // tmux session name
|
|
347
369
|
this.bridgeReady = false;
|
|
370
|
+
this._bridgeDisconnectErrorPromise = null;
|
|
348
371
|
// 0.12 Phase 1.6: claude-side MCP-server registration completion flag.
|
|
349
372
|
// Set true when bridge writes {kind:'mcp-ready'} after claude's first
|
|
350
373
|
// ListToolsRequest (Finding 0.3.A — cold-spawn race fix).
|
|
@@ -647,7 +670,11 @@ class CliProcess extends Process {
|
|
|
647
670
|
|
|
648
671
|
this.bridgeServer.on('bridge-message', msg => this._handleBridgeMessage(msg));
|
|
649
672
|
|
|
650
|
-
this.bridgeServer.on('bridge-disconnected', () =>
|
|
673
|
+
this.bridgeServer.on('bridge-disconnected', () => {
|
|
674
|
+
this._handleBridgeDisconnected().catch(err => {
|
|
675
|
+
this.logger.error?.(`[${this.label}] channels: bridge disconnect handling failed: ${err.message}`);
|
|
676
|
+
});
|
|
677
|
+
});
|
|
651
678
|
|
|
652
679
|
await this.bridgeServer.listen();
|
|
653
680
|
}
|
|
@@ -1005,8 +1032,8 @@ class CliProcess extends Process {
|
|
|
1005
1032
|
await this.runner.spawn({
|
|
1006
1033
|
name: tmuxName,
|
|
1007
1034
|
cwd: resolvedCwd || opts.cwd || process.cwd(),
|
|
1008
|
-
command: this.claudeBin,
|
|
1009
|
-
args: claudeArgs,
|
|
1035
|
+
command: this.sessionLauncher || this.claudeBin,
|
|
1036
|
+
args: this.sessionLauncher ? [this.claudeBin, ...claudeArgs] : claudeArgs,
|
|
1010
1037
|
envExtras: {
|
|
1011
1038
|
// Resume-dialog suppression (docs/0.13-resume-dialog-fix-spec.md B1):
|
|
1012
1039
|
// claude's session-age "resume-return" dialog fires when sessionAge ≥
|
|
@@ -2625,6 +2652,9 @@ class CliProcess extends Process {
|
|
|
2625
2652
|
// ─── public Process API ──────────────────────────────────────────
|
|
2626
2653
|
|
|
2627
2654
|
async send(prompt, opts = {}) {
|
|
2655
|
+
if (this._bridgeDisconnecting) {
|
|
2656
|
+
throw await this._bridgeDisconnectErrorPromise;
|
|
2657
|
+
}
|
|
2628
2658
|
if (this.closed) {
|
|
2629
2659
|
// Parity P21: PROCESS_CLOSED code for cross-backend symmetry.
|
|
2630
2660
|
const err = new Error('CliProcess: send on closed instance');
|
|
@@ -3786,10 +3816,13 @@ class CliProcess extends Process {
|
|
|
3786
3816
|
* landing just before the disconnect would otherwise leave a stray
|
|
3787
3817
|
* timer on the dead instance).
|
|
3788
3818
|
*/
|
|
3789
|
-
_handleBridgeDisconnected(reason = 'socket-close') {
|
|
3819
|
+
async _handleBridgeDisconnected(reason = 'socket-close') {
|
|
3790
3820
|
this.bridgeReady = false;
|
|
3791
3821
|
this.mcpReady = false;
|
|
3792
|
-
if (this.closed) return;
|
|
3822
|
+
if (this.closed || this._bridgeDisconnecting) return;
|
|
3823
|
+
this._bridgeDisconnecting = true;
|
|
3824
|
+
this._bridgeDisconnectErrorPromise = this._bridgeDisconnectError();
|
|
3825
|
+
const disconnectError = await this._bridgeDisconnectErrorPromise;
|
|
3793
3826
|
this._invalidatePendingDeliveryWork();
|
|
3794
3827
|
this.logger.warn?.(`[${this.label}] channels: bridge disconnected unexpectedly (${reason})`);
|
|
3795
3828
|
// L6: clear the interrupt grace timer alongside the rest of the lifecycle.
|
|
@@ -3807,9 +3840,7 @@ class CliProcess extends Process {
|
|
|
3807
3840
|
// L5: remove the per-turn stop-hook listener (this path bypasses
|
|
3808
3841
|
// Process.kill()'s removeAllListeners).
|
|
3809
3842
|
if (pending._onStop) this.off('stop-hook', pending._onStop);
|
|
3810
|
-
|
|
3811
|
-
err.code = 'BRIDGE_DISCONNECTED';
|
|
3812
|
-
try { pending.reject(err); } catch {}
|
|
3843
|
+
try { pending.reject(disconnectError); } catch {}
|
|
3813
3844
|
}
|
|
3814
3845
|
this.pendingTurns.clear();
|
|
3815
3846
|
this.pendingQueue.length = 0;
|
|
@@ -3820,8 +3851,36 @@ class CliProcess extends Process {
|
|
|
3820
3851
|
this._stopQuestionKeepAlive();
|
|
3821
3852
|
this._openQuestions.clear();
|
|
3822
3853
|
this._clearLedgerTimers(); // 0.13 D2
|
|
3823
|
-
this.emit('bridge-disconnected');
|
|
3824
|
-
this._logEvent('bridge-disconnected', { reason });
|
|
3854
|
+
this.emit('bridge-disconnected', { code: disconnectError.code });
|
|
3855
|
+
this._logEvent('bridge-disconnected', { reason, code: disconnectError.code });
|
|
3856
|
+
}
|
|
3857
|
+
|
|
3858
|
+
async _bridgeDisconnectError() {
|
|
3859
|
+
if (!this.sessionLauncher) {
|
|
3860
|
+
return Object.assign(new Error('bridge disconnected'), { code: 'BRIDGE_DISCONNECTED' });
|
|
3861
|
+
}
|
|
3862
|
+
|
|
3863
|
+
let processAlive = false;
|
|
3864
|
+
if (this.tmuxSession && typeof this.runner?.sessionProcessAlive === 'function') {
|
|
3865
|
+
let timeout;
|
|
3866
|
+
try {
|
|
3867
|
+
processAlive = await Promise.race([
|
|
3868
|
+
this.runner.sessionProcessAlive(this.tmuxSession),
|
|
3869
|
+
new Promise(resolve => {
|
|
3870
|
+
timeout = setTimeout(() => resolve(false), 250);
|
|
3871
|
+
timeout.unref?.();
|
|
3872
|
+
}),
|
|
3873
|
+
]);
|
|
3874
|
+
} catch (err) {
|
|
3875
|
+
this.logger.warn?.(`[${this.label}] channels: could not verify contained process after bridge disconnect: ${err.message}`);
|
|
3876
|
+
} finally {
|
|
3877
|
+
if (timeout) clearTimeout(timeout);
|
|
3878
|
+
}
|
|
3879
|
+
}
|
|
3880
|
+
if (processAlive) {
|
|
3881
|
+
return Object.assign(new Error('bridge disconnected'), { code: 'BRIDGE_DISCONNECTED' });
|
|
3882
|
+
}
|
|
3883
|
+
return Object.assign(new Error('contained session process was lost'), { code: 'SESSION_PROCESS_LOST' });
|
|
3825
3884
|
}
|
|
3826
3885
|
|
|
3827
3886
|
async _doKill(reason) {
|
package/lib/process/factory.js
CHANGED
|
@@ -105,6 +105,9 @@ function _maybeWarnR12Migration({ rawPm, canonical, chatId, threadId, chatCfg, t
|
|
|
105
105
|
* @param {string} [opts.channelsClaudeBin] — absolute path to pinned claude binary;
|
|
106
106
|
* required when ANY chat routes to 'cli'. (Name kept for back-compat with
|
|
107
107
|
* existing wiring; can be renamed to `claudeBin` in a future refactor.)
|
|
108
|
+
* @param {string|null} [opts.sessionLauncher] — optional absolute executable
|
|
109
|
+
* wrapper that receives the pinned Claude binary as its first argument. Defaults to
|
|
110
|
+
* ORCHESTRA_SESSION_LAUNCHER when configured by the consumer.
|
|
108
111
|
* @param {string|Function} [opts.displayHint] — surface-rendering hint prepended to
|
|
109
112
|
* the CliProcess system prompt. Accepts EITHER a static string (applied to every
|
|
110
113
|
* session) OR a resolver `(chatId, threadId, config) => string` invoked once per
|
|
@@ -127,6 +130,7 @@ function createProcessFactory({
|
|
|
127
130
|
botName = null,
|
|
128
131
|
toolDispatcher = null,
|
|
129
132
|
channelsClaudeBin = null,
|
|
133
|
+
sessionLauncher = process.env.ORCHESTRA_SESSION_LAUNCHER || null,
|
|
130
134
|
displayHint = '', // orchestra: consumer surface-rendering hint
|
|
131
135
|
maxOutboundFileBytes = 100 * 1024 * 1024, // orchestra: consumer outbound file cap
|
|
132
136
|
// orchestra identity — forwarded to every CliProcess so the engine is neutral.
|
|
@@ -174,6 +178,7 @@ function createProcessFactory({
|
|
|
174
178
|
tmuxRunner,
|
|
175
179
|
botName,
|
|
176
180
|
claudeBin: channelsClaudeBin,
|
|
181
|
+
sessionLauncher,
|
|
177
182
|
toolDispatcher,
|
|
178
183
|
displayHint: resolvedDisplayHint,
|
|
179
184
|
maxOutboundFileBytes,
|
package/lib/tmux/tmux-runner.js
CHANGED
|
@@ -188,8 +188,10 @@ function createTmuxRunner({
|
|
|
188
188
|
sessionPrefix = 'orchestra',
|
|
189
189
|
logger = console,
|
|
190
190
|
runFn = run,
|
|
191
|
+
requireExistingServer = false,
|
|
191
192
|
} = {}) {
|
|
192
193
|
const runnerPrefix = sessionPrefix;
|
|
194
|
+
const runTmux = (args, opts) => runFn('tmux', requireExistingServer ? ['-N', ...args] : args, opts);
|
|
193
195
|
async function spawn({
|
|
194
196
|
name,
|
|
195
197
|
cwd,
|
|
@@ -213,7 +215,7 @@ function createTmuxRunner({
|
|
|
213
215
|
}
|
|
214
216
|
sessArgs.push(command, ...args);
|
|
215
217
|
try {
|
|
216
|
-
await
|
|
218
|
+
await runTmux(sessArgs);
|
|
217
219
|
} catch (err) {
|
|
218
220
|
throw Object.assign(new Error(`tmux spawn failed: ${err.message}`), {
|
|
219
221
|
code: 'TMUX_SPAWN_FAILED',
|
|
@@ -229,7 +231,7 @@ function createTmuxRunner({
|
|
|
229
231
|
// settable option). capture-pane -J in captureWide() handles
|
|
230
232
|
// any remaining wrap artifacts.
|
|
231
233
|
try {
|
|
232
|
-
await
|
|
234
|
+
await runTmux(['resize-window', '-t', name, '-x', String(paneWidth)]);
|
|
233
235
|
} catch (err) {
|
|
234
236
|
logger.debug?.(`[tmux-runner] resize-window failed for ${name}: ${err.message} (capture-pane -J handles wrap)`);
|
|
235
237
|
}
|
|
@@ -242,7 +244,7 @@ function createTmuxRunner({
|
|
|
242
244
|
* key names like "Enter" and "C-c".
|
|
243
245
|
*/
|
|
244
246
|
async function sendControl(name, key) {
|
|
245
|
-
await
|
|
247
|
+
await runTmux(['send-keys', '-t', name, key]);
|
|
246
248
|
}
|
|
247
249
|
|
|
248
250
|
// rc.13.1: paste+Enter must be ATOMIC per session. Pre-rc.13.1 two
|
|
@@ -318,13 +320,13 @@ function createTmuxRunner({
|
|
|
318
320
|
const sanitized = sanitize(text);
|
|
319
321
|
const oneLine = sanitized.replace(/\r?\n/g, MULTILINE_SEPARATOR);
|
|
320
322
|
const bufName = `${runnerPrefix}-buf-${crypto.randomBytes(3).toString('hex')}`;
|
|
321
|
-
await
|
|
323
|
+
await runTmux(['set-buffer', '-b', bufName, oneLine]);
|
|
322
324
|
try {
|
|
323
325
|
// -d (delete after) so the buffer doesn't accumulate.
|
|
324
|
-
await
|
|
326
|
+
await runTmux(['paste-buffer', '-t', name, '-b', bufName, '-d']);
|
|
325
327
|
} catch (err) {
|
|
326
328
|
// Best-effort buffer cleanup if paste fails.
|
|
327
|
-
await
|
|
329
|
+
await runTmux(['delete-buffer', '-b', bufName]).catch(() => {});
|
|
328
330
|
throw err;
|
|
329
331
|
}
|
|
330
332
|
// Drain delay — see incident note above.
|
|
@@ -346,7 +348,7 @@ function createTmuxRunner({
|
|
|
346
348
|
const args = ['capture-pane', '-t', name, '-p'];
|
|
347
349
|
if (joinWrapped) args.push('-J');
|
|
348
350
|
args.push('-S', `-${lines}`);
|
|
349
|
-
const { stdout } = await
|
|
351
|
+
const { stdout } = await runTmux(args);
|
|
350
352
|
return stdout;
|
|
351
353
|
}
|
|
352
354
|
|
|
@@ -360,15 +362,33 @@ function createTmuxRunner({
|
|
|
360
362
|
|
|
361
363
|
async function sessionExists(name) {
|
|
362
364
|
try {
|
|
363
|
-
await
|
|
365
|
+
await runTmux(['has-session', '-t', name]);
|
|
364
366
|
return true;
|
|
365
367
|
} catch {
|
|
366
368
|
return false;
|
|
367
369
|
}
|
|
368
370
|
}
|
|
369
371
|
|
|
372
|
+
async function sessionProcessAlive(name) {
|
|
373
|
+
try {
|
|
374
|
+
const { stdout } = await runTmux([
|
|
375
|
+
'list-panes',
|
|
376
|
+
'-t',
|
|
377
|
+
name,
|
|
378
|
+
'-F',
|
|
379
|
+
'#{pane_dead} #{pane_pid}',
|
|
380
|
+
]);
|
|
381
|
+
return stdout
|
|
382
|
+
.trim()
|
|
383
|
+
.split('\n')
|
|
384
|
+
.some(line => /^0 [1-9][0-9]*$/.test(line.trim()));
|
|
385
|
+
} catch {
|
|
386
|
+
return false;
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
370
390
|
async function killSession(name) {
|
|
371
|
-
await
|
|
391
|
+
await runTmux(['kill-session', '-t', name]).catch(() => {});
|
|
372
392
|
}
|
|
373
393
|
|
|
374
394
|
/**
|
|
@@ -377,7 +397,7 @@ function createTmuxRunner({
|
|
|
377
397
|
*/
|
|
378
398
|
async function listPolygramSessions(botName = null) {
|
|
379
399
|
try {
|
|
380
|
-
const { stdout } = await
|
|
400
|
+
const { stdout } = await runTmux(['list-sessions', '-F', '#{session_name}']);
|
|
381
401
|
const all = stdout.trim().split('\n').filter(Boolean);
|
|
382
402
|
const prefix = botName ? `${runnerPrefix}-${String(botName).replace(/[^\w-]/g, '_')}-` : `${runnerPrefix}-`;
|
|
383
403
|
return all.filter((n) => n.startsWith(prefix));
|
|
@@ -394,6 +414,7 @@ function createTmuxRunner({
|
|
|
394
414
|
capturePane,
|
|
395
415
|
captureWide,
|
|
396
416
|
sessionExists,
|
|
417
|
+
sessionProcessAlive,
|
|
397
418
|
killSession,
|
|
398
419
|
listPolygramSessions,
|
|
399
420
|
sessionName: (b, c, t) => sessionName(b, c, t, sessionPrefix),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shumkov/orchestra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Drive interactive Claude Code CLI sessions: spawn, inject messages via the MCP channels bridge, observe turns, recover. The transport-agnostic session engine extracted from polygram, shared by polygram (Telegram) and water (WhatsApp).",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "index.js",
|