@shumkov/orchestra 0.13.1 → 0.14.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.
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Per-session Claude CLI context window and prompt cache
|
|
2
|
+
|
|
3
|
+
A consumer can choose the auto-compaction window and prompt-cache lifetime for
|
|
4
|
+
each chat or topic, even when sessions share a tmux server. Orchestra accepts
|
|
5
|
+
normalized values. The consumer owns configuration parsing and inheritance.
|
|
6
|
+
|
|
7
|
+
| Factory or spawn-context option | Accepted value | Claude CLI environment variable |
|
|
8
|
+
| --- | --- | --- |
|
|
9
|
+
| `autoCompactWindow` | Safe integer from 100,000 through 1,000,000 tokens | `CLAUDE_CODE_AUTO_COMPACT_WINDOW` |
|
|
10
|
+
| `promptCacheTtl` | `'5m'` or `'1h'` | `CLAUDE_CODE_PROMPT_CACHE_TTL` |
|
|
11
|
+
|
|
12
|
+
Both options also accept `undefined` and `null`. For each option independently,
|
|
13
|
+
a defined spawn-context value takes precedence over the factory option. The
|
|
14
|
+
factory accepts a static value or a synchronous `(chatId, threadId, config)`
|
|
15
|
+
resolver. Only the Claude CLI backend evaluates these resolvers.
|
|
16
|
+
|
|
17
|
+
| Resolved value | tmux session environment |
|
|
18
|
+
| --- | --- |
|
|
19
|
+
| Omitted or `undefined` | Inherit the variable from tmux as before |
|
|
20
|
+
| `null` | Set the variable to an empty string so Claude applies its normal defaults or settings |
|
|
21
|
+
| Concrete value | Set the variable to the exact token count or TTL |
|
|
22
|
+
|
|
23
|
+
An invalid value raises a `TypeError` during CLI process construction. The tmux
|
|
24
|
+
runner accepts environment names matching `^[A-Za-z_][A-Za-z0-9_]*$` and string
|
|
25
|
+
values free of NUL characters. Codex rejects either Claude-specific context key
|
|
26
|
+
on presence, including a key with an undefined value. Typed options keep
|
|
27
|
+
validation and reload detection local to the CLI backend. A generic environment
|
|
28
|
+
map would expose unrelated variables. Server-wide settings would affect every chat.
|
|
29
|
+
|
|
30
|
+
Consumers that support live changes supply the resolved values on each spawn
|
|
31
|
+
context. A changed, defined value triggers the existing idle reload and resume
|
|
32
|
+
path. An active turn finishes before its next idle dispatch can apply a change.
|
|
33
|
+
An omitted option requests no reload for that field. The reload event records
|
|
34
|
+
the old and new values for each changed option. Proactive compaction warnings
|
|
35
|
+
measure transcript usage against the configured window or the existing default.
|
|
36
|
+
|
|
37
|
+
Unit tests cover constructor bounds, exact spawn environment, tmux argument
|
|
38
|
+
validation, resolver precedence, Codex isolation, idle reload and reuse, reload
|
|
39
|
+
telemetry and the warning threshold. Provider cache usage and actual CLI
|
|
40
|
+
compaction timing can be measured after consumer rollout.
|
|
@@ -37,12 +37,18 @@ const DEFAULT_CLOSE_GRACE_MS = 1_000;
|
|
|
37
37
|
const DEFAULT_CLOSE_KILL_MS = 1_000;
|
|
38
38
|
const CODEX_SUPERVISOR_GRACE_MS =
|
|
39
39
|
DEFAULT_CLOSE_GRACE_MS + DEFAULT_CLOSE_KILL_MS;
|
|
40
|
-
|
|
40
|
+
// Codex caps a command's aggregatedOutput at about 1 MiB, and JSON escaping
|
|
41
|
+
// can grow that up to six times in its item/completed line (a control
|
|
42
|
+
// character becomes \u00XX). The line limit must sit above that worst case.
|
|
43
|
+
const DEFAULT_MAX_LINE_BYTES = 8 * 1024 * 1024;
|
|
41
44
|
const DEFAULT_MAX_STDERR_BYTES = 64 * 1024;
|
|
42
45
|
const DEFAULT_MAX_PENDING_REQUESTS = 64;
|
|
43
46
|
const DEFAULT_MAX_USED_REQUEST_IDS = 65_536;
|
|
44
47
|
const DEFAULT_MAX_QUEUED_LINES = 256;
|
|
45
|
-
|
|
48
|
+
// At least twice the line limit: stdout resumes below half this cap, so one
|
|
49
|
+
// maximum-size line on top of a queue just under that mark still stays under
|
|
50
|
+
// the cap, which a chunk then only reaches if the stream ignored pause().
|
|
51
|
+
const DEFAULT_MAX_QUEUED_BYTES = 2 * DEFAULT_MAX_LINE_BYTES;
|
|
46
52
|
const DEFAULT_SINK_TIMEOUT_MS = 5_000;
|
|
47
53
|
const CLIENT_ROOT_ERROR_CODES = Object.freeze([
|
|
48
54
|
'CODEX_PROTOCOL_ERROR',
|
|
@@ -317,6 +317,8 @@ class CliProcess extends Process {
|
|
|
317
317
|
// hint (appended to the system prompt) and outbound file-size cap, so the engine
|
|
318
318
|
// is transport-agnostic. Were hard `require`s in the polygram/water copy.
|
|
319
319
|
displayHint = '',
|
|
320
|
+
autoCompactWindow,
|
|
321
|
+
promptCacheTtl,
|
|
320
322
|
maxOutboundFileBytes = 100 * 1024 * 1024,
|
|
321
323
|
// orchestra identity options — the consumer's names, so the engine is neutral.
|
|
322
324
|
// sessionPrefix: tmux session + socket + mcp-config file prefix (MUST match the
|
|
@@ -378,6 +380,17 @@ class CliProcess extends Process {
|
|
|
378
380
|
// spawn context's string hint, so a null (from a resolver that returned
|
|
379
381
|
// one) would be drift no context could ever match — a permanent respawn.
|
|
380
382
|
this.displayHint = typeof displayHint === 'string' ? displayHint : '';
|
|
383
|
+
if (autoCompactWindow !== undefined && autoCompactWindow !== null && (
|
|
384
|
+
!Number.isSafeInteger(autoCompactWindow) || autoCompactWindow < 100_000 || autoCompactWindow > 1_000_000
|
|
385
|
+
)) {
|
|
386
|
+
throw new TypeError('autoCompactWindow must be null or a safe integer from 100000 to 1000000');
|
|
387
|
+
}
|
|
388
|
+
if (promptCacheTtl !== undefined && promptCacheTtl !== null
|
|
389
|
+
&& promptCacheTtl !== '5m' && promptCacheTtl !== '1h') {
|
|
390
|
+
throw new TypeError("promptCacheTtl must be null, '5m' or '1h'");
|
|
391
|
+
}
|
|
392
|
+
this.autoCompactWindow = autoCompactWindow;
|
|
393
|
+
this.promptCacheTtl = promptCacheTtl;
|
|
381
394
|
this._injectedMaxOutboundFileBytes = maxOutboundFileBytes;
|
|
382
395
|
this.sessionPrefix = sessionPrefix;
|
|
383
396
|
this.bridgeServerName = bridgeServerName;
|
|
@@ -1311,26 +1324,20 @@ class CliProcess extends Process {
|
|
|
1311
1324
|
// same fallback chain. The `--add-dir` flag is independent: it
|
|
1312
1325
|
// declares an additional trusted-roots entry, NOT the launch dir.
|
|
1313
1326
|
//
|
|
1314
|
-
//
|
|
1327
|
+
// Suppress Claude's resume-age dialog, whose default action compacts the session.
|
|
1328
|
+
const envExtras = { CLAUDE_CODE_RESUME_THRESHOLD_MINUTES: '525600' };
|
|
1329
|
+
if (this.autoCompactWindow !== undefined) {
|
|
1330
|
+
envExtras.CLAUDE_CODE_AUTO_COMPACT_WINDOW = this.autoCompactWindow === null ? '' : String(this.autoCompactWindow);
|
|
1331
|
+
}
|
|
1332
|
+
if (this.promptCacheTtl !== undefined) {
|
|
1333
|
+
envExtras.CLAUDE_CODE_PROMPT_CACHE_TTL = this.promptCacheTtl === null ? '' : this.promptCacheTtl;
|
|
1334
|
+
}
|
|
1315
1335
|
await this.runner.spawn({
|
|
1316
1336
|
name: tmuxName,
|
|
1317
1337
|
cwd: resolvedCwd || opts.cwd || process.cwd(),
|
|
1318
1338
|
command: this.sessionLauncher || this.claudeBin,
|
|
1319
1339
|
args: this.sessionLauncher ? [this.claudeBin, ...claudeArgs] : claudeArgs,
|
|
1320
|
-
envExtras
|
|
1321
|
-
// Resume-dialog suppression (docs/0.13-resume-dialog-fix-spec.md B1):
|
|
1322
|
-
// claude's session-age "resume-return" dialog fires when sessionAge ≥
|
|
1323
|
-
// this many minutes AND est. tokens ≥ CLAUDE_CODE_RESUME_TOKEN_THRESHOLD
|
|
1324
|
-
// (defaults 70 / 1e5, binary-verified on 2.1.158). Its pre-selected
|
|
1325
|
-
// option literally runs /compact — silently compacting every aged
|
|
1326
|
-
// --resume (and breaking the /model "conversation kept" guarantee).
|
|
1327
|
-
// A huge threshold (1 year) means the dialog never triggers and resume
|
|
1328
|
-
// is always full-session-as-is. Per-process env — the operator's own
|
|
1329
|
-
// interactive claude is untouched. Belt-and-braces: the session-age
|
|
1330
|
-
// gate trigger below still navigates to "full" if a future binary bump
|
|
1331
|
-
// renames this var.
|
|
1332
|
-
CLAUDE_CODE_RESUME_THRESHOLD_MINUTES: '525600',
|
|
1333
|
-
},
|
|
1340
|
+
envExtras,
|
|
1334
1341
|
});
|
|
1335
1342
|
|
|
1336
1343
|
// Dialog handling (Phase 0 finding) — poll capture-pane and Enter through:
|
|
@@ -4361,6 +4368,12 @@ class CliProcess extends Process {
|
|
|
4361
4368
|
typeof spawnContext?.displayHint === 'string'
|
|
4362
4369
|
&& spawnContext.displayHint !== this.displayHint
|
|
4363
4370
|
) reasons.push('display-hint');
|
|
4371
|
+
if (spawnContext?.autoCompactWindow !== undefined && spawnContext.autoCompactWindow !== this.autoCompactWindow) {
|
|
4372
|
+
reasons.push('auto-compact-window');
|
|
4373
|
+
}
|
|
4374
|
+
if (spawnContext?.promptCacheTtl !== undefined && spawnContext.promptCacheTtl !== this.promptCacheTtl) {
|
|
4375
|
+
reasons.push('prompt-cache-ttl');
|
|
4376
|
+
}
|
|
4364
4377
|
return reasons;
|
|
4365
4378
|
}
|
|
4366
4379
|
|
|
@@ -5808,7 +5821,7 @@ class CliProcess extends Process {
|
|
|
5808
5821
|
if (!this.compactionWarn?.enabled || this._compactionWarned) return;
|
|
5809
5822
|
const usage = await readContextTokens(transcriptPath);
|
|
5810
5823
|
if (!usage) return;
|
|
5811
|
-
const pct = contextPct(usage.total) * 100;
|
|
5824
|
+
const pct = contextPct(usage.total, this.autoCompactWindow ?? undefined) * 100;
|
|
5812
5825
|
if (pct < this.compactionWarn.thresholdPct) return;
|
|
5813
5826
|
if (this._compactionWarned) return; // re-check after the async gap
|
|
5814
5827
|
this._compactionWarned = true;
|
package/lib/process/factory.js
CHANGED
|
@@ -53,6 +53,8 @@ const FORBIDDEN_CODEX_CONTEXT_FIELDS = new Set([
|
|
|
53
53
|
'cwd',
|
|
54
54
|
'model',
|
|
55
55
|
'effort',
|
|
56
|
+
'autoCompactWindow',
|
|
57
|
+
'promptCacheTtl',
|
|
56
58
|
'env',
|
|
57
59
|
'sessionLauncher',
|
|
58
60
|
'binary',
|
|
@@ -464,6 +466,10 @@ function _maybeWarnR12Migration({ rawPm, canonical, chatId, threadId, chatCfg, t
|
|
|
464
466
|
* here would make them two, and a one-character disagreement would respawn the
|
|
465
467
|
* session on every message forever. The resolver remains the fallback for consumers
|
|
466
468
|
* that send no hint on the context.
|
|
469
|
+
* @param {number|null|Function} [opts.autoCompactWindow] — CLI auto-compact window in tokens.
|
|
470
|
+
* @param {'5m'|'1h'|null|Function} [opts.promptCacheTtl] — CLI prompt cache lifetime.
|
|
471
|
+
* Both accept a static value or a (chatId, threadId, config) resolver. A defined
|
|
472
|
+
* spawn-context value overrides either form, including null to clear the variable.
|
|
467
473
|
* @param {Function} [opts.codexClientFactory] — trusted Codex app-server client
|
|
468
474
|
* factory. Required only when spawnContext.runtime is exactly 'codex'.
|
|
469
475
|
* @param {Function} [opts.codexCheckpointSink] — acknowledged durability sink.
|
|
@@ -489,6 +495,8 @@ function createProcessFactory({
|
|
|
489
495
|
channelsClaudeBin = null,
|
|
490
496
|
sessionLauncher = process.env.ORCHESTRA_SESSION_LAUNCHER || null,
|
|
491
497
|
displayHint = '', // orchestra: consumer surface-rendering hint
|
|
498
|
+
autoCompactWindow,
|
|
499
|
+
promptCacheTtl,
|
|
492
500
|
maxOutboundFileBytes = 100 * 1024 * 1024, // orchestra: consumer outbound file cap
|
|
493
501
|
// orchestra identity — forwarded to every CliProcess so the engine is neutral.
|
|
494
502
|
sessionPrefix = 'orchestra',
|
|
@@ -565,18 +573,17 @@ function createProcessFactory({
|
|
|
565
573
|
// hint on the context: a static string (same hint for every session) or
|
|
566
574
|
// a function called per spawn with the current chat/topic and config, so
|
|
567
575
|
// the consumer can vary the hint per chat (e.g. a rich-text toggle).
|
|
576
|
+
const resolveOption = option => typeof option === 'function'
|
|
577
|
+
? option(chatId, threadId, config)
|
|
578
|
+
: option;
|
|
568
579
|
const resolvedDisplayHint = typeof ctx?.displayHint === 'string'
|
|
569
580
|
? ctx.displayHint
|
|
570
|
-
:
|
|
571
|
-
? displayHint(chatId, threadId, config)
|
|
572
|
-
: displayHint;
|
|
581
|
+
: resolveOption(displayHint);
|
|
573
582
|
// Same string-or-resolver shape as displayHint: the optional tools a
|
|
574
583
|
// consumer can honor may vary per chat/topic (a live preview is a
|
|
575
584
|
// per-chat rollout decision), and the prompt section that teaches them
|
|
576
585
|
// is resolved at the same moment, so the two can never disagree.
|
|
577
|
-
const resolvedCapabilities =
|
|
578
|
-
? toolDispatcherCapabilities(chatId, threadId, config)
|
|
579
|
-
: toolDispatcherCapabilities;
|
|
586
|
+
const resolvedCapabilities = resolveOption(toolDispatcherCapabilities);
|
|
580
587
|
return new CliProcess({
|
|
581
588
|
sessionKey, chatId, threadId, label,
|
|
582
589
|
tmuxRunner,
|
|
@@ -586,6 +593,10 @@ function createProcessFactory({
|
|
|
586
593
|
toolDispatcher,
|
|
587
594
|
toolDispatcherCapabilities: resolvedCapabilities,
|
|
588
595
|
displayHint: resolvedDisplayHint,
|
|
596
|
+
autoCompactWindow: ctx?.autoCompactWindow !== undefined
|
|
597
|
+
? ctx.autoCompactWindow : resolveOption(autoCompactWindow),
|
|
598
|
+
promptCacheTtl: ctx?.promptCacheTtl !== undefined
|
|
599
|
+
? ctx.promptCacheTtl : resolveOption(promptCacheTtl),
|
|
589
600
|
maxOutboundFileBytes,
|
|
590
601
|
sessionPrefix, bridgeServerName, appDataDir, attachmentBase, productName, surfaceName,
|
|
591
602
|
logger,
|
|
@@ -957,6 +957,14 @@ class ProcessManager {
|
|
|
957
957
|
from_hint_hash: hintFingerprint(existing.displayHint),
|
|
958
958
|
to_hint_hash: hintFingerprint(spawnContext?.displayHint),
|
|
959
959
|
}),
|
|
960
|
+
...(reasons?.includes('auto-compact-window') && {
|
|
961
|
+
from_auto_compact_window: existing.autoCompactWindow ?? null,
|
|
962
|
+
to_auto_compact_window: spawnContext.autoCompactWindow,
|
|
963
|
+
}),
|
|
964
|
+
...(reasons?.includes('prompt-cache-ttl') && {
|
|
965
|
+
from_prompt_cache_ttl: existing.promptCacheTtl ?? null,
|
|
966
|
+
to_prompt_cache_ttl: spawnContext.promptCacheTtl,
|
|
967
|
+
}),
|
|
960
968
|
});
|
|
961
969
|
await this._killLocked(sessionKey, 'config-reload');
|
|
962
970
|
this._throwIfShuttingDown();
|
package/lib/tmux/tmux-runner.js
CHANGED
|
@@ -239,6 +239,12 @@ function createTmuxRunner({
|
|
|
239
239
|
const sessArgs = ['new-session', '-d', '-s', name];
|
|
240
240
|
if (cwd) sessArgs.push('-c', cwd);
|
|
241
241
|
for (const [k, v] of Object.entries(envExtras)) {
|
|
242
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(k)) {
|
|
243
|
+
throw new TypeError(`Invalid envExtras key: ${JSON.stringify(k)}`);
|
|
244
|
+
}
|
|
245
|
+
if (typeof v !== 'string' || v.includes('\0')) {
|
|
246
|
+
throw new TypeError(`envExtras.${k} must be a string without NUL characters`);
|
|
247
|
+
}
|
|
242
248
|
sessArgs.push('-e', `${k}=${v}`);
|
|
243
249
|
}
|
|
244
250
|
sessArgs.push(command, ...args);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shumkov/orchestra",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.14.0",
|
|
4
4
|
"description": "Transport-agnostic session engine for Claude Code and the native Codex app-server: spawn, steer, supervise, observe turns, and recover.",
|
|
5
5
|
"type": "commonjs",
|
|
6
6
|
"main": "index.js",
|