@adhdev/session-host-core 1.0.82-rc.21 → 1.0.82-rc.23
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/dist/index.d.mts +48 -1
- package/dist/index.d.ts +48 -1
- package/dist/index.js +100 -9
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +93 -9
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/cli-child-priority.ts +81 -0
- package/src/index.ts +3 -0
- package/src/process/hidden-spawn.ts +10 -0
- package/src/spawn-env.ts +11 -0
- package/src/types.ts +2 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import * as os from 'node:os';
|
|
2
|
+
import { hiddenSpawn } from './process/hidden-spawn.js';
|
|
3
|
+
|
|
4
|
+
/** Relative POSIX niceness adjustment for newly spawned agent workloads. */
|
|
5
|
+
export const CLI_CHILD_NICE_INCREMENT = 10;
|
|
6
|
+
/** Set to "0" in the daemon environment (or launch env) to disable lowering. */
|
|
7
|
+
export const CLI_CHILD_NICE_ENV = 'ADHDEV_CLI_CHILD_NICE';
|
|
8
|
+
/** Optional Darwin CPU + I/O background policy, in addition to baseline nice. */
|
|
9
|
+
export const CLI_CHILD_SCHEDULING_ENV = 'ADHDEV_CLI_CHILD_SCHEDULING';
|
|
10
|
+
|
|
11
|
+
function setDarwinBackground(pid: number, onFailure: (error: unknown) => void): void {
|
|
12
|
+
// A short control helper stays normal priority. Do not block the IPC owner.
|
|
13
|
+
const task = hiddenSpawn('/usr/sbin/taskpolicy', ['-b', '-p', String(pid)], { stdio: 'ignore' });
|
|
14
|
+
task.once('error', onFailure);
|
|
15
|
+
task.once('exit', (code, signal) => {
|
|
16
|
+
if (code !== 0) onFailure(new Error(`taskpolicy exited with ${signal ?? code}`));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface PriorityDependencies {
|
|
21
|
+
platform: NodeJS.Platform;
|
|
22
|
+
getPriority: typeof os.getPriority;
|
|
23
|
+
setPriority: typeof os.setPriority;
|
|
24
|
+
setDarwinBackground?: typeof setDarwinBackground;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Separate policy instances are useful for platform/error tests. */
|
|
28
|
+
export function createCliChildPriorityPolicy(deps: PriorityDependencies = {
|
|
29
|
+
platform: process.platform,
|
|
30
|
+
getPriority: os.getPriority,
|
|
31
|
+
setPriority: os.setPriority,
|
|
32
|
+
setDarwinBackground,
|
|
33
|
+
}) {
|
|
34
|
+
let failureLogged = false;
|
|
35
|
+
return function spawnCliChild<T extends { readonly pid?: number }>(
|
|
36
|
+
spawnChild: () => T,
|
|
37
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
38
|
+
logInfo: (message: string) => void = console.info,
|
|
39
|
+
): T {
|
|
40
|
+
const child = spawnChild();
|
|
41
|
+
const pid = child.pid;
|
|
42
|
+
// Never pass 0/undefined to setPriority: that would lower the IPC owner!
|
|
43
|
+
if (env[CLI_CHILD_NICE_ENV] === '0' || typeof pid !== 'number' || !Number.isInteger(pid) || pid <= 0) return child;
|
|
44
|
+
const onFailure = (error: unknown) => {
|
|
45
|
+
if (!failureLogged) {
|
|
46
|
+
failureLogged = true;
|
|
47
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
48
|
+
logInfo(`[cli-child-priority] Could not apply CLI child scheduling (pid ${pid}); continuing: ${detail}`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
try {
|
|
52
|
+
const priority = deps.platform === 'win32'
|
|
53
|
+
? os.constants.priority.PRIORITY_BELOW_NORMAL
|
|
54
|
+
: Math.min(19, deps.getPriority(pid) + CLI_CHILD_NICE_INCREMENT);
|
|
55
|
+
deps.setPriority(pid, priority);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
// EACCES/EPERM (policy/permissions) and ESRCH (already exited), as well
|
|
58
|
+
// as unexpected OS errors, are best effort. Never fail a CLI launch.
|
|
59
|
+
onFailure(error);
|
|
60
|
+
}
|
|
61
|
+
if (deps.platform === 'darwin' && env[CLI_CHILD_SCHEDULING_ENV] === 'darwin-bg') {
|
|
62
|
+
try { deps.setDarwinBackground?.(pid, onFailure); } catch (error) { onFailure(error); }
|
|
63
|
+
}
|
|
64
|
+
return child;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Only wrap agent workload spawns. Keep daemon, session-host IPC, P2P signal
|
|
70
|
+
* workers, version probes and git helpers at their existing priority.
|
|
71
|
+
*
|
|
72
|
+
* node-pty has no pre-spawn priority hook. Apply immediately on return, before
|
|
73
|
+
* registering listeners/doing other work. There is a spawn-to-setPriority race:
|
|
74
|
+
* a child can run (and fork) before this call. Later descendants inherit nice
|
|
75
|
+
* on POSIX; early descendants are not retroactively changed. Windows creation
|
|
76
|
+
* and ConPTY inheritance require verification on the MainPC.
|
|
77
|
+
* Optional darwin-bg is applied asynchronously by taskpolicy after nice; the
|
|
78
|
+
* extra helper startup adds a further race before the background class takes
|
|
79
|
+
* effect. It also throttles I/O, so nice alone remains the default.
|
|
80
|
+
*/
|
|
81
|
+
export const spawnCliChild = createCliChildPriorityPolicy();
|
package/src/index.ts
CHANGED
|
@@ -50,6 +50,8 @@ export type {
|
|
|
50
50
|
|
|
51
51
|
export { SESSION_HOST_SUPPORTED_REQUEST_TYPES } from './types.js';
|
|
52
52
|
export { classifyTermination } from './termination.js';
|
|
53
|
+
export { CLI_CHILD_NICE_INCREMENT, CLI_CHILD_NICE_ENV, CLI_CHILD_SCHEDULING_ENV, createCliChildPriorityPolicy, spawnCliChild } from './cli-child-priority.js';
|
|
54
|
+
export { hiddenSpawn as hiddenSpawnProcess } from './process/hidden-spawn.js';
|
|
53
55
|
|
|
54
56
|
export { DEFAULT_SESSION_RING_BUFFER_MAX_BYTES, SessionRingBuffer } from './buffer.js';
|
|
55
57
|
export type { SessionRingBufferOptions } from './buffer.js';
|
|
@@ -99,6 +101,7 @@ export {
|
|
|
99
101
|
} from './defaults.js';
|
|
100
102
|
export {
|
|
101
103
|
sanitizeSpawnEnv,
|
|
104
|
+
replayLaunchEnv,
|
|
102
105
|
applyTerminalColorEnv,
|
|
103
106
|
ensureNodePtySpawnHelperPermissions,
|
|
104
107
|
} from './spawn-env.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { spawn, type SpawnOptions, type ChildProcess } from 'node:child_process';
|
|
2
|
+
|
|
3
|
+
/** Shared hidden-window primitive for daemon and session-host process helpers. */
|
|
4
|
+
export function hiddenSpawn(
|
|
5
|
+
command: string,
|
|
6
|
+
args: readonly string[] = [],
|
|
7
|
+
options: SpawnOptions = {},
|
|
8
|
+
): ChildProcess {
|
|
9
|
+
return spawn(command, args as string[], { windowsHide: true, ...options });
|
|
10
|
+
}
|
package/src/spawn-env.ts
CHANGED
|
@@ -117,6 +117,17 @@ export function sanitizeSpawnEnv(
|
|
|
117
117
|
return env;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
+
/** Recover only role bindings from legacy full-environment snapshots.
|
|
121
|
+
* New records distinguish explicit overrides, including intentional custom keys. */
|
|
122
|
+
export function replayLaunchEnv(command: { env?: Record<string, string>; envMode?: 'launch_overrides' }): Record<string, string> {
|
|
123
|
+
if (command.envMode === 'launch_overrides') return { ...command.env };
|
|
124
|
+
const roleKeys = new Set(['HOME', 'USERPROFILE', 'XDG_CONFIG_HOME', 'CLAUDE_CONFIG_DIR',
|
|
125
|
+
'CODEX_HOME', 'KIMI_CODE_HOME', 'GEMINI_CLI_HOME', 'OPENCODE_CONFIG', 'OPENCODE_CONFIG_CONTENT',
|
|
126
|
+
'CLAUDE_CODE_DISABLE_AUTO_MEMORY', 'TERMINAL_CWD', 'ADHDEV_INLINE_MESH',
|
|
127
|
+
'ADHDEV_ASSISTANT_SESSION_ID', 'ADHDEV_COORDINATOR_SESSION_ID', 'ADHDEV_CONFIG_DIR']);
|
|
128
|
+
return Object.fromEntries(Object.entries(command.env ?? {}).filter(([key]) => roleKeys.has(key)));
|
|
129
|
+
}
|
|
130
|
+
|
|
120
131
|
/**
|
|
121
132
|
* Apply preferred terminal colour environment variables.
|
|
122
133
|
* Ensures TERM is set to xterm-256color and enables colour on Windows.
|
package/src/types.ts
CHANGED
|
@@ -12,6 +12,8 @@ export interface SessionLaunchCommand {
|
|
|
12
12
|
command: string;
|
|
13
13
|
args: string[];
|
|
14
14
|
env?: Record<string, string>;
|
|
15
|
+
/** Unmarked older daemon records contained the full inherited environment. */
|
|
16
|
+
envMode?: 'launch_overrides';
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export interface SessionWriteOwner {
|