shellpuppet 0.0.1 → 0.1.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/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +340 -8
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +693 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client/http.d.ts +82 -0
- package/dist/client/http.js +225 -0
- package/dist/client/http.js.map +1 -0
- package/dist/core/dialogs.d.ts +88 -0
- package/dist/core/dialogs.js +214 -0
- package/dist/core/dialogs.js.map +1 -0
- package/dist/core/echo.d.ts +71 -0
- package/dist/core/echo.js +249 -0
- package/dist/core/echo.js.map +1 -0
- package/dist/core/find.d.ts +34 -0
- package/dist/core/find.js +41 -0
- package/dist/core/find.js.map +1 -0
- package/dist/core/keys.d.ts +22 -0
- package/dist/core/keys.js +80 -0
- package/dist/core/keys.js.map +1 -0
- package/dist/core/manager.d.ts +66 -0
- package/dist/core/manager.js +158 -0
- package/dist/core/manager.js.map +1 -0
- package/dist/core/nativePty.d.ts +69 -0
- package/dist/core/nativePty.js +156 -0
- package/dist/core/nativePty.js.map +1 -0
- package/dist/core/policy.d.ts +61 -0
- package/dist/core/policy.js +143 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/promptDetector.d.ts +75 -0
- package/dist/core/promptDetector.js +606 -0
- package/dist/core/promptDetector.js.map +1 -0
- package/dist/core/reasoning.d.ts +252 -0
- package/dist/core/reasoning.js +740 -0
- package/dist/core/reasoning.js.map +1 -0
- package/dist/core/regexBudget.d.ts +44 -0
- package/dist/core/regexBudget.js +79 -0
- package/dist/core/regexBudget.js.map +1 -0
- package/dist/core/renderer.d.ts +112 -0
- package/dist/core/renderer.js +225 -0
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/session.d.ts +311 -0
- package/dist/core/session.js +966 -0
- package/dist/core/session.js.map +1 -0
- package/dist/core/types.d.ts +281 -0
- package/dist/core/types.js +47 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/waiter.d.ts +86 -0
- package/dist/core/waiter.js +317 -0
- package/dist/core/waiter.js.map +1 -0
- package/dist/core/windowsPtyStart.d.ts +90 -0
- package/dist/core/windowsPtyStart.js +129 -0
- package/dist/core/windowsPtyStart.js.map +1 -0
- package/dist/daemon/audit.d.ts +23 -0
- package/dist/daemon/audit.js +51 -0
- package/dist/daemon/audit.js.map +1 -0
- package/dist/daemon/auth.d.ts +127 -0
- package/dist/daemon/auth.js +241 -0
- package/dist/daemon/auth.js.map +1 -0
- package/dist/daemon/config.d.ts +76 -0
- package/dist/daemon/config.js +301 -0
- package/dist/daemon/config.js.map +1 -0
- package/dist/daemon/index.d.ts +7 -0
- package/dist/daemon/index.js +87 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/liveness.d.ts +36 -0
- package/dist/daemon/liveness.js +71 -0
- package/dist/daemon/liveness.js.map +1 -0
- package/dist/daemon/logger.d.ts +7 -0
- package/dist/daemon/logger.js +29 -0
- package/dist/daemon/logger.js.map +1 -0
- package/dist/daemon/meta.d.ts +9 -0
- package/dist/daemon/meta.js +24 -0
- package/dist/daemon/meta.js.map +1 -0
- package/dist/daemon/paths.d.ts +83 -0
- package/dist/daemon/paths.js +154 -0
- package/dist/daemon/paths.js.map +1 -0
- package/dist/daemon/screenshot.d.ts +87 -0
- package/dist/daemon/screenshot.js +256 -0
- package/dist/daemon/screenshot.js.map +1 -0
- package/dist/daemon/server.d.ts +244 -0
- package/dist/daemon/server.js +2187 -0
- package/dist/daemon/server.js.map +1 -0
- package/dist/daemon/terminalReports.d.ts +38 -0
- package/dist/daemon/terminalReports.js +84 -0
- package/dist/daemon/terminalReports.js.map +1 -0
- package/dist/daemon/viewerLauncher.d.ts +135 -0
- package/dist/daemon/viewerLauncher.js +280 -0
- package/dist/daemon/viewerLauncher.js.map +1 -0
- package/dist/daemon/viewerSync.d.ts +38 -0
- package/dist/daemon/viewerSync.js +83 -0
- package/dist/daemon/viewerSync.js.map +1 -0
- package/dist/mcp/config.d.ts +57 -0
- package/dist/mcp/config.js +106 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +69 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/tools.d.ts +59 -0
- package/dist/mcp/tools.js +537 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/viewer/icon-16.png +0 -0
- package/dist/viewer/icon-180.png +0 -0
- package/dist/viewer/icon-32.png +0 -0
- package/dist/viewer/icon-512.png +0 -0
- package/dist/viewer/icon.svg +66 -0
- package/dist/viewer/index.html +56 -0
- package/dist/viewer/viewer.css +84 -0
- package/dist/viewer/viewer.js +661 -0
- package/package.json +66 -7
- package/index.js +0 -4
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic key names → terminal escape sequences.
|
|
3
|
+
*
|
|
4
|
+
* Agents are bad at raw escape sequences, so the tool surface exposes readable
|
|
5
|
+
* names (`enter`, `escape`, `ctrl+c`, `f5`…), mapped to the sequences xterm
|
|
6
|
+
* sends. `shift+tab` (backtab) is included because TUI focus navigation needs
|
|
7
|
+
* it.
|
|
8
|
+
*
|
|
9
|
+
* Pure data + lookup. No I/O.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Translate a semantic key name to the byte sequence to write to the PTY.
|
|
13
|
+
* Names are case-insensitive and surrounding whitespace is ignored.
|
|
14
|
+
* Returns `null` for names that are not recognised, and callers turn that into
|
|
15
|
+
* a clear client error rather than writing garbage into the session.
|
|
16
|
+
*/
|
|
17
|
+
export declare function keyToBytes(key: string): string | null;
|
|
18
|
+
/**
|
|
19
|
+
* Every accepted key name, for tool schemas and error messages. The patterned
|
|
20
|
+
* forms are described rather than enumerated.
|
|
21
|
+
*/
|
|
22
|
+
export declare function knownKeys(): string[];
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Semantic key names → terminal escape sequences.
|
|
3
|
+
*
|
|
4
|
+
* Agents are bad at raw escape sequences, so the tool surface exposes readable
|
|
5
|
+
* names (`enter`, `escape`, `ctrl+c`, `f5`…), mapped to the sequences xterm
|
|
6
|
+
* sends. `shift+tab` (backtab) is included because TUI focus navigation needs
|
|
7
|
+
* it.
|
|
8
|
+
*
|
|
9
|
+
* Pure data + lookup. No I/O.
|
|
10
|
+
*/
|
|
11
|
+
const NAMED_KEYS = {
|
|
12
|
+
enter: '\r',
|
|
13
|
+
tab: '\t',
|
|
14
|
+
'shift+tab': '\x1b[Z',
|
|
15
|
+
escape: '\x1b',
|
|
16
|
+
esc: '\x1b',
|
|
17
|
+
backspace: '\x7f',
|
|
18
|
+
delete: '\x1b[3~',
|
|
19
|
+
space: ' ',
|
|
20
|
+
up: '\x1b[A',
|
|
21
|
+
down: '\x1b[B',
|
|
22
|
+
right: '\x1b[C',
|
|
23
|
+
left: '\x1b[D',
|
|
24
|
+
arrow_up: '\x1b[A',
|
|
25
|
+
arrow_down: '\x1b[B',
|
|
26
|
+
arrow_right: '\x1b[C',
|
|
27
|
+
arrow_left: '\x1b[D',
|
|
28
|
+
home: '\x1b[H',
|
|
29
|
+
end: '\x1b[F',
|
|
30
|
+
pageup: '\x1b[5~',
|
|
31
|
+
pagedown: '\x1b[6~',
|
|
32
|
+
};
|
|
33
|
+
/** F1 to F4 use SS3 codes; F5+ use CSI codes, as xterm does. */
|
|
34
|
+
const FUNCTION_KEYS = {
|
|
35
|
+
1: '\x1bOP',
|
|
36
|
+
2: '\x1bOQ',
|
|
37
|
+
3: '\x1bOR',
|
|
38
|
+
4: '\x1bOS',
|
|
39
|
+
5: '\x1b[15~',
|
|
40
|
+
6: '\x1b[17~',
|
|
41
|
+
7: '\x1b[18~',
|
|
42
|
+
8: '\x1b[19~',
|
|
43
|
+
9: '\x1b[20~',
|
|
44
|
+
10: '\x1b[21~',
|
|
45
|
+
11: '\x1b[23~',
|
|
46
|
+
12: '\x1b[24~',
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Translate a semantic key name to the byte sequence to write to the PTY.
|
|
50
|
+
* Names are case-insensitive and surrounding whitespace is ignored.
|
|
51
|
+
* Returns `null` for names that are not recognised, and callers turn that into
|
|
52
|
+
* a clear client error rather than writing garbage into the session.
|
|
53
|
+
*/
|
|
54
|
+
export function keyToBytes(key) {
|
|
55
|
+
const name = key.toLowerCase().trim();
|
|
56
|
+
const named = NAMED_KEYS[name];
|
|
57
|
+
if (named !== undefined)
|
|
58
|
+
return named;
|
|
59
|
+
// ctrl+a … ctrl+z → 0x01 … 0x1a
|
|
60
|
+
const ctrl = /^ctrl\+([a-z])$/.exec(name);
|
|
61
|
+
if (ctrl !== null && ctrl[1] !== undefined) {
|
|
62
|
+
return String.fromCharCode(ctrl[1].charCodeAt(0) - 96);
|
|
63
|
+
}
|
|
64
|
+
// f1 … f12
|
|
65
|
+
const fn = /^f(\d{1,2})$/.exec(name);
|
|
66
|
+
if (fn !== null && fn[1] !== undefined) {
|
|
67
|
+
const sequence = FUNCTION_KEYS[Number(fn[1])];
|
|
68
|
+
if (sequence !== undefined)
|
|
69
|
+
return sequence;
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Every accepted key name, for tool schemas and error messages. The patterned
|
|
75
|
+
* forms are described rather than enumerated.
|
|
76
|
+
*/
|
|
77
|
+
export function knownKeys() {
|
|
78
|
+
return [...Object.keys(NAMED_KEYS), 'ctrl+<a-z>', 'f1-f12'];
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=keys.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keys.js","sourceRoot":"","sources":["../../src/core/keys.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,UAAU,GAAqC;IACnD,KAAK,EAAE,IAAI;IACX,GAAG,EAAE,IAAI;IACT,WAAW,EAAE,QAAQ;IACrB,MAAM,EAAE,MAAM;I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,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,WAAW;IACX,MAAM,EAAE,GAAG,cAAc,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrC,IAAI,EAAE,KAAK,IAAI,IAAI,EAAE,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE,CAAC;QACvC,MAAM,QAAQ,GAAG,aAAa,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAC9C,IAAI,QAAQ,KAAK,SAAS;YAAE,OAAO,QAAQ,CAAC;IAC9C,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,SAAS;IACvB,OAAO,CAAC,GAAG,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,EAAE,YAAY,EAAE,QAAQ,CAAC,CAAC;AAC9D,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session registry: ids, policy enforcement, cleanup (design §2.4).
|
|
3
|
+
*
|
|
4
|
+
* The manager is where policy meets the pre-wrap command (design §4.3): it
|
|
5
|
+
* evaluates `command args…` exactly as the caller passed them, and only then
|
|
6
|
+
* hands off to `Session`, which applies the platform shell wrapper at spawn.
|
|
7
|
+
*/
|
|
8
|
+
import { Session } from './session.js';
|
|
9
|
+
import type { SessionDeps } from './session.js';
|
|
10
|
+
import type { CompiledPolicy } from './policy.js';
|
|
11
|
+
import type { Logger, SessionOptions, SessionSummary } from './types.js';
|
|
12
|
+
export declare class PolicyViolationError extends Error {
|
|
13
|
+
constructor(message: string);
|
|
14
|
+
}
|
|
15
|
+
export declare class SessionManager {
|
|
16
|
+
private readonly sessions;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly sessionDeps;
|
|
19
|
+
private policy;
|
|
20
|
+
private seq;
|
|
21
|
+
constructor(policy: CompiledPolicy, logger?: Logger, deps?: SessionDeps);
|
|
22
|
+
/** Replace the policy. */
|
|
23
|
+
setPolicy(policy: CompiledPolicy): void;
|
|
24
|
+
/**
|
|
25
|
+
* Policy-check and start a session. Throws `PolicyViolationError` with the
|
|
26
|
+
* policy's reason when refused. Callers map it to a 403.
|
|
27
|
+
*/
|
|
28
|
+
create(options: SessionOptions): Session;
|
|
29
|
+
get(id: string): Session | undefined;
|
|
30
|
+
list(): SessionSummary[];
|
|
31
|
+
/** Sessions whose process has not exited. */
|
|
32
|
+
liveCount(): number;
|
|
33
|
+
size(): number;
|
|
34
|
+
/**
|
|
35
|
+
* Reap sessions whose process exited more than `graceMs` ago, so a natural
|
|
36
|
+
* exit releases the renderer and its scrollback just as a kill does,
|
|
37
|
+
* rather than holding them until daemon shutdown. Agents legitimately read the
|
|
38
|
+
* final screen after exit, so exited sessions stay readable during the
|
|
39
|
+
* grace and their resources are released after it. Returns the
|
|
40
|
+
* reaped ids so the daemon layer can revoke their viewer grants and
|
|
41
|
+
* credentials and close their watcher sockets. Kill (`DELETE`) is immediate.
|
|
42
|
+
*/
|
|
43
|
+
reapExited(graceMs: number): Promise<string[]>;
|
|
44
|
+
/** Tree-kill one session and drop it from the registry. */
|
|
45
|
+
kill(id: string): Promise<boolean>;
|
|
46
|
+
/**
|
|
47
|
+
* Tree-kill everything at daemon shutdown (design §2.3). The daemon must
|
|
48
|
+
* never exit and strand running agent CLIs.
|
|
49
|
+
*
|
|
50
|
+
* On Windows every live session's tree is killed first, in one `taskkill`
|
|
51
|
+
* run (`taskkillTrees` says why), so each session's own kill then finds its
|
|
52
|
+
* root gone and only has its terminal left to release. That run is bounded
|
|
53
|
+
* by its own cap (`TREE_KILL_TIMEOUT_MS`), not by `timeoutMs`: a shorter
|
|
54
|
+
* bound would cut short the very run it exists for. So shutdown takes
|
|
55
|
+
* at most that cap plus `timeoutMs`.
|
|
56
|
+
*
|
|
57
|
+
* Each kill is bounded so shutdown ALWAYS completes. An unbounded teardown
|
|
58
|
+
* that wedges leaves the daemon alive but undiscoverable. That is worse than
|
|
59
|
+
* a best-effort kill, because the user can then only end it via Task
|
|
60
|
+
* Manager, and every future client sees a stale state file. A kill that
|
|
61
|
+
* overruns is logged and abandoned.
|
|
62
|
+
*/
|
|
63
|
+
killAll(timeoutMs?: number): Promise<void>;
|
|
64
|
+
/** Windows only: kill every live session's tree in one `taskkill` run. */
|
|
65
|
+
private killTreesInOneRun;
|
|
66
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session registry: ids, policy enforcement, cleanup (design §2.4).
|
|
3
|
+
*
|
|
4
|
+
* The manager is where policy meets the pre-wrap command (design §4.3): it
|
|
5
|
+
* evaluates `command args…` exactly as the caller passed them, and only then
|
|
6
|
+
* hands off to `Session`, which applies the platform shell wrapper at spawn.
|
|
7
|
+
*/
|
|
8
|
+
import { Session, TREE_KILL_TIMEOUT_MS, taskkillTrees } from './session.js';
|
|
9
|
+
import { checkSessionCapacity, evaluateCommand } from './policy.js';
|
|
10
|
+
import { NULL_LOGGER } from './types.js';
|
|
11
|
+
export class PolicyViolationError extends Error {
|
|
12
|
+
constructor(message) {
|
|
13
|
+
super(message);
|
|
14
|
+
this.name = 'PolicyViolationError';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
export class SessionManager {
|
|
18
|
+
sessions = new Map();
|
|
19
|
+
logger;
|
|
20
|
+
sessionDeps;
|
|
21
|
+
policy;
|
|
22
|
+
seq = 0;
|
|
23
|
+
constructor(policy, logger = NULL_LOGGER, deps = {}) {
|
|
24
|
+
this.policy = policy;
|
|
25
|
+
this.logger = logger;
|
|
26
|
+
this.sessionDeps = { logger, ...deps };
|
|
27
|
+
}
|
|
28
|
+
/** Replace the policy. */
|
|
29
|
+
setPolicy(policy) {
|
|
30
|
+
this.policy = policy;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Policy-check and start a session. Throws `PolicyViolationError` with the
|
|
34
|
+
* policy's reason when refused. Callers map it to a 403.
|
|
35
|
+
*/
|
|
36
|
+
create(options) {
|
|
37
|
+
const decision = evaluateCommand(this.policy, options.command, options.args ?? []);
|
|
38
|
+
if (!decision.allowed) {
|
|
39
|
+
this.logger.warn('session refused by policy', {
|
|
40
|
+
command: options.command,
|
|
41
|
+
reason: decision.reason,
|
|
42
|
+
});
|
|
43
|
+
throw new PolicyViolationError(decision.reason ?? 'Command refused by policy.');
|
|
44
|
+
}
|
|
45
|
+
const capacity = checkSessionCapacity(this.policy, this.liveCount());
|
|
46
|
+
if (!capacity.allowed) {
|
|
47
|
+
throw new PolicyViolationError(capacity.reason ?? 'Session limit reached.');
|
|
48
|
+
}
|
|
49
|
+
const id = `s${String(++this.seq)}`;
|
|
50
|
+
const session = new Session(id, options, this.sessionDeps);
|
|
51
|
+
this.sessions.set(id, session);
|
|
52
|
+
return session;
|
|
53
|
+
}
|
|
54
|
+
get(id) {
|
|
55
|
+
return this.sessions.get(id);
|
|
56
|
+
}
|
|
57
|
+
list() {
|
|
58
|
+
return [...this.sessions.values()].map((s) => s.summary());
|
|
59
|
+
}
|
|
60
|
+
/** Sessions whose process has not exited. */
|
|
61
|
+
liveCount() {
|
|
62
|
+
return [...this.sessions.values()].filter((s) => !s.exited).length;
|
|
63
|
+
}
|
|
64
|
+
size() {
|
|
65
|
+
return this.sessions.size;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Reap sessions whose process exited more than `graceMs` ago, so a natural
|
|
69
|
+
* exit releases the renderer and its scrollback just as a kill does,
|
|
70
|
+
* rather than holding them until daemon shutdown. Agents legitimately read the
|
|
71
|
+
* final screen after exit, so exited sessions stay readable during the
|
|
72
|
+
* grace and their resources are released after it. Returns the
|
|
73
|
+
* reaped ids so the daemon layer can revoke their viewer grants and
|
|
74
|
+
* credentials and close their watcher sockets. Kill (`DELETE`) is immediate.
|
|
75
|
+
*/
|
|
76
|
+
async reapExited(graceMs) {
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
const reaped = [];
|
|
79
|
+
for (const [id, session] of [...this.sessions]) {
|
|
80
|
+
if (!session.exited || session.exitedAt === null)
|
|
81
|
+
continue;
|
|
82
|
+
if (now - session.exitedAt < graceMs)
|
|
83
|
+
continue;
|
|
84
|
+
this.sessions.delete(id);
|
|
85
|
+
// The process is already dead; kill() just releases the PTY handles
|
|
86
|
+
// and disposes the renderer. It deliberately does NOT tree-kill: the
|
|
87
|
+
// root has been gone for the whole grace period, so there is nothing
|
|
88
|
+
// to walk the tree from and the process group id may have been
|
|
89
|
+
// recycled. A grandchild that outlived its parent is left
|
|
90
|
+
// running, which is a residual risk stated in
|
|
91
|
+
// `docs/reference/security-model.md` rather than a case to guess at.
|
|
92
|
+
await session.kill();
|
|
93
|
+
reaped.push(id);
|
|
94
|
+
}
|
|
95
|
+
return reaped;
|
|
96
|
+
}
|
|
97
|
+
/** Tree-kill one session and drop it from the registry. */
|
|
98
|
+
async kill(id) {
|
|
99
|
+
const session = this.sessions.get(id);
|
|
100
|
+
if (session === undefined)
|
|
101
|
+
return false;
|
|
102
|
+
await session.kill();
|
|
103
|
+
this.sessions.delete(id);
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Tree-kill everything at daemon shutdown (design §2.3). The daemon must
|
|
108
|
+
* never exit and strand running agent CLIs.
|
|
109
|
+
*
|
|
110
|
+
* On Windows every live session's tree is killed first, in one `taskkill`
|
|
111
|
+
* run (`taskkillTrees` says why), so each session's own kill then finds its
|
|
112
|
+
* root gone and only has its terminal left to release. That run is bounded
|
|
113
|
+
* by its own cap (`TREE_KILL_TIMEOUT_MS`), not by `timeoutMs`: a shorter
|
|
114
|
+
* bound would cut short the very run it exists for. So shutdown takes
|
|
115
|
+
* at most that cap plus `timeoutMs`.
|
|
116
|
+
*
|
|
117
|
+
* Each kill is bounded so shutdown ALWAYS completes. An unbounded teardown
|
|
118
|
+
* that wedges leaves the daemon alive but undiscoverable. That is worse than
|
|
119
|
+
* a best-effort kill, because the user can then only end it via Task
|
|
120
|
+
* Manager, and every future client sees a stale state file. A kill that
|
|
121
|
+
* overruns is logged and abandoned.
|
|
122
|
+
*/
|
|
123
|
+
async killAll(timeoutMs = 10_000) {
|
|
124
|
+
const all = [...this.sessions.values()];
|
|
125
|
+
this.sessions.clear();
|
|
126
|
+
await this.killTreesInOneRun(all);
|
|
127
|
+
await Promise.all(all.map(async (session) => {
|
|
128
|
+
let timer;
|
|
129
|
+
const bound = new Promise((resolve) => {
|
|
130
|
+
timer = setTimeout(() => resolve('timeout'), timeoutMs);
|
|
131
|
+
});
|
|
132
|
+
const outcome = await Promise.race([session.kill(), bound]);
|
|
133
|
+
if (timer !== undefined)
|
|
134
|
+
clearTimeout(timer);
|
|
135
|
+
if (outcome === 'timeout') {
|
|
136
|
+
this.logger.warn('session teardown exceeded its budget, abandoning it', {
|
|
137
|
+
id: session.id,
|
|
138
|
+
timeoutMs,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
143
|
+
/** Windows only: kill every live session's tree in one `taskkill` run. */
|
|
144
|
+
async killTreesInOneRun(sessions) {
|
|
145
|
+
if ((this.sessionDeps.platform ?? process.platform) !== 'win32')
|
|
146
|
+
return;
|
|
147
|
+
const pids = sessions.filter((session) => !session.exited).map((session) => session.pid);
|
|
148
|
+
if (pids.length === 0)
|
|
149
|
+
return;
|
|
150
|
+
const failure = await (this.sessionDeps.treeKiller ?? taskkillTrees)(pids);
|
|
151
|
+
if (failure !== null) {
|
|
152
|
+
this.logger.warn(failure.timedOut
|
|
153
|
+
? 'shutdown tree kill timed out; each session still running is killed on its own'
|
|
154
|
+
: 'shutdown taskkill exited non-zero (usually descendants that vanished mid-walk)', { sessions: pids.length, ...failure, timeoutMs: TREE_KILL_TIMEOUT_MS });
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=manager.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.js","sourceRoot":"","sources":["../../src/core/manager.ts"],"names":[],"mappings":"A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,CAAC;YACrB,IAAI,CAAC,MAAM,CAAC,IAAI,CACd,OAAO,CAAC,QAAQ;gBACd,CAAC,CAAC,+EAA+E;gBACjF,CAAC,CAAC,gFAAgF,EACpF,EAAE,QAAQ,EAAE,IAAI,CAAC,MAAM,EAAE,GAAG,OAAO,EAAE,SAAS,EAAE,oBAAoB,EAAE,CACvE,CAAC;QACJ,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loading node-pty, and the ways an npm install can leave it unable to open a
|
|
3
|
+
* terminal.
|
|
4
|
+
*
|
|
5
|
+
* npm 12 runs a dependency's install scripts only when the person installing
|
|
6
|
+
* allows them. node-pty has prebuilt binaries for Windows and macOS but not for
|
|
7
|
+
* Linux, where its install script builds one; without that script there is no
|
|
8
|
+
* native module to load. On macOS the prebuilt `spawn-helper` ships without its
|
|
9
|
+
* execute bit (microsoft/node-pty#850). npm 12 would block an install script
|
|
10
|
+
* that restored it, and every spawn would then fail with "posix_spawnp failed".
|
|
11
|
+
*
|
|
12
|
+
* So node-pty is loaded on the first start rather than when the daemon starts,
|
|
13
|
+
* and the helper's execute bit is checked there. Either failure reaches the
|
|
14
|
+
* person as a start that fails with what to do, instead of a daemon that dies
|
|
15
|
+
* before it answers and a client that can only say it never became healthy.
|
|
16
|
+
* On Windows node-pty loads its native module when a terminal is built, not
|
|
17
|
+
* when it is imported, so a spawn's failure gets the same explanation.
|
|
18
|
+
*/
|
|
19
|
+
type NodePty = typeof import('node-pty');
|
|
20
|
+
/**
|
|
21
|
+
* The install that builds node-pty under npm 12. Earlier npm runs the script
|
|
22
|
+
* without the flag; npm 10 and early npm 11 releases ignore it as an unknown
|
|
23
|
+
* option.
|
|
24
|
+
*/
|
|
25
|
+
export declare const REINSTALL = "npm install -g --allow-scripts=node-pty shellpuppet";
|
|
26
|
+
/** node-pty cannot open a terminal on this install; the message says what to do. */
|
|
27
|
+
export declare class NativePtyUnavailableError extends Error {
|
|
28
|
+
constructor(message: string);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* A loader that keeps node-pty once a load succeeds, and explains a failure. A
|
|
32
|
+
* failed load is not remembered, so a start after a reinstall tries again.
|
|
33
|
+
* `load` is a test seam.
|
|
34
|
+
*/
|
|
35
|
+
export declare function nodePtyLoader(load?: () => NodePty): () => NodePty;
|
|
36
|
+
/** The daemon's node-pty. */
|
|
37
|
+
export declare const loadNodePty: () => NodePty;
|
|
38
|
+
/**
|
|
39
|
+
* What a node-pty load failure means for the person who hit it. node-pty's own
|
|
40
|
+
* loader reports a missing module as "Failed to load native module: pty.node,
|
|
41
|
+
* checked: …"; a module built for another Node.js names NODE_MODULE_VERSION,
|
|
42
|
+
* on a later line of the message than the first.
|
|
43
|
+
*/
|
|
44
|
+
export declare function explainLoadFailure(error: unknown): string;
|
|
45
|
+
/**
|
|
46
|
+
* A spawn's failure, explained when it is node-pty's native module that would
|
|
47
|
+
* not load (on Windows that happens as a terminal is built). Anything else is
|
|
48
|
+
* returned as it was, so other start failures keep their own handling.
|
|
49
|
+
*/
|
|
50
|
+
export declare function explainSpawnFailure(error: unknown): unknown;
|
|
51
|
+
/** The file operations `ensureSpawnHelperExecutable` needs; a test seam. */
|
|
52
|
+
export interface HelperFs {
|
|
53
|
+
/** Whether this native module loads, as node-pty's loader would find. */
|
|
54
|
+
loads(file: string): boolean;
|
|
55
|
+
isExecutable(file: string): boolean;
|
|
56
|
+
makeExecutable(file: string): void;
|
|
57
|
+
}
|
|
58
|
+
/** The real file operations; exported so a test can pair them with a stand-in `loads`. */
|
|
59
|
+
export declare const realHelperFs: HelperFs;
|
|
60
|
+
/**
|
|
61
|
+
* On macOS, make sure node-pty's `spawn-helper` can run before a spawn needs
|
|
62
|
+
* it. node-pty uses the helper beside the native module it loaded. Its loader
|
|
63
|
+
* tries `build/Release`, `build/Debug` and then `prebuilds/<platform>-<arch>`,
|
|
64
|
+
* each first beside its `lib` directory and then inside it, and moves on when
|
|
65
|
+
* a module fails to load. This follows the same order and takes the first
|
|
66
|
+
* module that loads. Anywhere but macOS it does nothing.
|
|
67
|
+
*/
|
|
68
|
+
export declare function ensureSpawnHelperExecutable(root?: string, platform?: NodeJS.Platform, arch?: string, ops?: HelperFs): void;
|
|
69
|
+
export {};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Loading node-pty, and the ways an npm install can leave it unable to open a
|
|
3
|
+
* terminal.
|
|
4
|
+
*
|
|
5
|
+
* npm 12 runs a dependency's install scripts only when the person installing
|
|
6
|
+
* allows them. node-pty has prebuilt binaries for Windows and macOS but not for
|
|
7
|
+
* Linux, where its install script builds one; without that script there is no
|
|
8
|
+
* native module to load. On macOS the prebuilt `spawn-helper` ships without its
|
|
9
|
+
* execute bit (microsoft/node-pty#850). npm 12 would block an install script
|
|
10
|
+
* that restored it, and every spawn would then fail with "posix_spawnp failed".
|
|
11
|
+
*
|
|
12
|
+
* So node-pty is loaded on the first start rather than when the daemon starts,
|
|
13
|
+
* and the helper's execute bit is checked there. Either failure reaches the
|
|
14
|
+
* person as a start that fails with what to do, instead of a daemon that dies
|
|
15
|
+
* before it answers and a client that can only say it never became healthy.
|
|
16
|
+
* On Windows node-pty loads its native module when a terminal is built, not
|
|
17
|
+
* when it is imported, so a spawn's failure gets the same explanation.
|
|
18
|
+
*/
|
|
19
|
+
import * as fs from 'node:fs';
|
|
20
|
+
import { createRequire } from 'node:module';
|
|
21
|
+
import * as path from 'node:path';
|
|
22
|
+
const require = createRequire(import.meta.url);
|
|
23
|
+
/**
|
|
24
|
+
* The install that builds node-pty under npm 12. Earlier npm runs the script
|
|
25
|
+
* without the flag; npm 10 and early npm 11 releases ignore it as an unknown
|
|
26
|
+
* option.
|
|
27
|
+
*/
|
|
28
|
+
export const REINSTALL = 'npm install -g --allow-scripts=node-pty shellpuppet';
|
|
29
|
+
/** node-pty cannot open a terminal on this install; the message says what to do. */
|
|
30
|
+
export class NativePtyUnavailableError extends Error {
|
|
31
|
+
constructor(message) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.name = 'NativePtyUnavailableError';
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* A loader that keeps node-pty once a load succeeds, and explains a failure. A
|
|
38
|
+
* failed load is not remembered, so a start after a reinstall tries again.
|
|
39
|
+
* `load` is a test seam.
|
|
40
|
+
*/
|
|
41
|
+
export function nodePtyLoader(load = () => require('node-pty')) {
|
|
42
|
+
let loaded;
|
|
43
|
+
return () => {
|
|
44
|
+
if (loaded === undefined) {
|
|
45
|
+
try {
|
|
46
|
+
loaded = load();
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
throw new NativePtyUnavailableError(explainLoadFailure(error));
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return loaded;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** The daemon's node-pty. */
|
|
56
|
+
export const loadNodePty = nodePtyLoader();
|
|
57
|
+
/**
|
|
58
|
+
* What a node-pty load failure means for the person who hit it. node-pty's own
|
|
59
|
+
* loader reports a missing module as "Failed to load native module: pty.node,
|
|
60
|
+
* checked: …"; a module built for another Node.js names NODE_MODULE_VERSION,
|
|
61
|
+
* on a later line of the message than the first.
|
|
62
|
+
*/
|
|
63
|
+
export function explainLoadFailure(error) {
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
// Classified whole, shown by its first line: Node appends the whole require
|
|
66
|
+
// stack to a missing module's message, which is noise to the person reading
|
|
67
|
+
// it, but the reason a module would not load can sit on a later line.
|
|
68
|
+
const detail = message.split('\n')[0] ?? '';
|
|
69
|
+
if (/NODE_MODULE_VERSION|compiled against a different Node\.js version/.test(message)) {
|
|
70
|
+
return ('node-pty was built for a different version of Node.js, so ShellPuppet cannot open a terminal. ' +
|
|
71
|
+
`Reinstall ShellPuppet under this Node.js: ${REINSTALL}. (${detail})`);
|
|
72
|
+
}
|
|
73
|
+
// node-pty's loader keeps only its last error, so a module built for
|
|
74
|
+
// another Node.js can look like a missing one here. Say both.
|
|
75
|
+
if (/Failed to load native module|Cannot find module/.test(message)) {
|
|
76
|
+
return ("node-pty's native module is missing, or was built for another version of Node.js, " +
|
|
77
|
+
'so ShellPuppet cannot open a terminal. ' +
|
|
78
|
+
"npm 12 runs a dependency's install script only when you allow it; reinstall with " +
|
|
79
|
+
`${REINSTALL} (on Linux that builds node-pty, which needs Python 3, make and a C++ compiler). (${detail})`);
|
|
80
|
+
}
|
|
81
|
+
return `node-pty could not be loaded, so ShellPuppet cannot open a terminal: ${detail}`;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* A spawn's failure, explained when it is node-pty's native module that would
|
|
85
|
+
* not load (on Windows that happens as a terminal is built). Anything else is
|
|
86
|
+
* returned as it was, so other start failures keep their own handling.
|
|
87
|
+
*/
|
|
88
|
+
export function explainSpawnFailure(error) {
|
|
89
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
90
|
+
return /Failed to load native module/.test(message) ? new NativePtyUnavailableError(explainLoadFailure(error)) : error;
|
|
91
|
+
}
|
|
92
|
+
/** The real file operations; exported so a test can pair them with a stand-in `loads`. */
|
|
93
|
+
export const realHelperFs = {
|
|
94
|
+
loads: (file) => {
|
|
95
|
+
try {
|
|
96
|
+
require(file);
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
isExecutable: (file) => {
|
|
104
|
+
try {
|
|
105
|
+
fs.accessSync(file, fs.constants.X_OK);
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
catch {
|
|
109
|
+
return false;
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
makeExecutable: (file) => {
|
|
113
|
+
fs.chmodSync(file, 0o755);
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
/** A path as one POSIX shell word, safe to paste: single quotes, with any single quote escaped. */
|
|
117
|
+
function shellQuote(value) {
|
|
118
|
+
return `'${value.replace(/'/g, `'\\''`)}'`;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* On macOS, make sure node-pty's `spawn-helper` can run before a spawn needs
|
|
122
|
+
* it. node-pty uses the helper beside the native module it loaded. Its loader
|
|
123
|
+
* tries `build/Release`, `build/Debug` and then `prebuilds/<platform>-<arch>`,
|
|
124
|
+
* each first beside its `lib` directory and then inside it, and moves on when
|
|
125
|
+
* a module fails to load. This follows the same order and takes the first
|
|
126
|
+
* module that loads. Anywhere but macOS it does nothing.
|
|
127
|
+
*/
|
|
128
|
+
export function ensureSpawnHelperExecutable(root = path.dirname(require.resolve('node-pty/package.json')), platform = process.platform, arch = process.arch, ops = realHelperFs) {
|
|
129
|
+
if (platform !== 'darwin')
|
|
130
|
+
return;
|
|
131
|
+
for (const dir of ['build/Release', 'build/Debug', `prebuilds/${platform}-${arch}`]) {
|
|
132
|
+
for (const base of [root, path.join(root, 'lib')]) {
|
|
133
|
+
const candidate = path.join(base, dir);
|
|
134
|
+
if (!ops.loads(path.join(candidate, 'pty.node')))
|
|
135
|
+
continue;
|
|
136
|
+
const helper = path.join(candidate, 'spawn-helper');
|
|
137
|
+
if (ops.isExecutable(helper))
|
|
138
|
+
return;
|
|
139
|
+
try {
|
|
140
|
+
ops.makeExecutable(helper);
|
|
141
|
+
}
|
|
142
|
+
catch (error) {
|
|
143
|
+
const code = error.code ?? String(error);
|
|
144
|
+
if (code === 'ENOENT') {
|
|
145
|
+
throw new NativePtyUnavailableError(`node-pty's spawn-helper is missing (${helper}), so ShellPuppet cannot open a terminal. ` +
|
|
146
|
+
`Reinstall ShellPuppet: ${REINSTALL}.`);
|
|
147
|
+
}
|
|
148
|
+
throw new NativePtyUnavailableError(`node-pty's spawn-helper is not executable and ShellPuppet could not make it so (${code}), ` +
|
|
149
|
+
`so it cannot open a terminal. Run: chmod +x -- ${shellQuote(helper)} ` +
|
|
150
|
+
'(with sudo if ShellPuppet was installed as root).');
|
|
151
|
+
}
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
//# sourceMappingURL=nativePty.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativePty.js","sourceRoot":"","sources":["../../src/core/nativePty.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,MAAM,SAAS,CAAC;AAC9B,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AAIlC,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,qDAAqD,CAAC;AAE/E,oFAAoF;AACpF,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAClD,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,2BAA2B,CAAC;IAC1C,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,OAAsB,GAAG,EAAE,CAAC,OAAO,CAAC,UAAU,CAAY;IACtF,IAAI,MAA2B,CAAC;IAChC,OAAO,GAAG,EAAE;QACV,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;YACzB,IAAI,CAAC;gBACH,MAAM,GAAG,IAAI,EAAE,CAAC;YAClB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,IAAI,yBAAyB,CAAC,kBAAkB,CAAC,KAAK,CAAC,CAAC,CAAC;YACjE,CAAC;QACH,CAAC;QACD,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC;AAED,6BAA6B;AAC7B,MAAM,CAAC,MAAM,WAAW,GAAG,aAAa,EAAE,CAAC;AAE3C;;;;;GAKG;AACH,MAAM,U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MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC;YACpD,IAAI,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC;gBAAE,OAAO;YACrC,IAAI,CAAC;gBACH,GAAG,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;YAC7B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,MAAM,IAAI,GAAI,KAA+B,CAAC,IAAI,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC;gBACpE,IAAI,IAAI,KAAK,QAAQ,EAAE,CAAC;oBACtB,MAAM,IAAI,yBAAyB,CACjC,uCAAuC,MAAM,4CAA4C;wBACvF,0BAA0B,SAAS,GAAG,CACzC,CAAC;gBACJ,CAAC;gBACD,MAAM,IAAI,yBAAyB,CACjC,mFAAmF,IAAI,KAAK;oBAC1F,kDAAkD,UAAU,CAAC,MAAM,CAAC,GAAG;oBACvE,mDAAmD,CACtD,CAAC;YACJ,CAAC;YACD,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Command policy: allow/deny evaluation for `terminal_start` (design §4.3).
|
|
3
|
+
*
|
|
4
|
+
* Two invariants:
|
|
5
|
+
*
|
|
6
|
+
* 1. **Policy evaluates the pre-wrap command.** Callers pass the real command
|
|
7
|
+
* (`goose session`); the daemon applies the Windows `cmd.exe /c` wrapper
|
|
8
|
+
* *after* the policy decision. If policy saw wrapped commands, an allowlist
|
|
9
|
+
* would see `cmd.exe` for everything and be unsatisfiable.
|
|
10
|
+
* 2. **Configuration errors fail closed.** A malformed pattern in an active
|
|
11
|
+
* policy denies every command with a clear reason, rather than silently
|
|
12
|
+
* not matching. A policy that cannot be evaluated as written must not be
|
|
13
|
+
* guessed at.
|
|
14
|
+
*
|
|
15
|
+
* Policy applies to commands only. It deliberately does not filter keystrokes:
|
|
16
|
+
* that is unenforceable theatre once a shell is running, and pretending
|
|
17
|
+
* otherwise would be worse than saying so.
|
|
18
|
+
*
|
|
19
|
+
* Pure evaluation. No I/O: config loading lives daemon-side.
|
|
20
|
+
*/
|
|
21
|
+
import type { PolicyConfig, PolicyDecision, PolicyMode } from './types.js';
|
|
22
|
+
interface CompiledRule {
|
|
23
|
+
source: string;
|
|
24
|
+
regex: RegExp;
|
|
25
|
+
}
|
|
26
|
+
export interface CompiledPolicy {
|
|
27
|
+
mode: PolicyMode;
|
|
28
|
+
allow: CompiledRule[];
|
|
29
|
+
deny: CompiledRule[];
|
|
30
|
+
/** Concurrent live sessions allowed; no limit when undefined. */
|
|
31
|
+
maxSessions: number | undefined;
|
|
32
|
+
/** Pattern sources that failed to compile. Non-empty ⇒ active policy denies all. */
|
|
33
|
+
invalidPatterns: string[];
|
|
34
|
+
/**
|
|
35
|
+
* Present ⇒ the policy section itself could not be parsed as written (set
|
|
36
|
+
* by the daemon's config validation, invariant 2). Deny everything,
|
|
37
|
+
* regardless of mode: a bad SHAPE cannot even tell us the intended mode.
|
|
38
|
+
*/
|
|
39
|
+
configError?: string;
|
|
40
|
+
}
|
|
41
|
+
/** The exact string patterns are matched against: command + args, space-joined. */
|
|
42
|
+
export declare function commandLine(command: string, args?: string[]): string;
|
|
43
|
+
/**
|
|
44
|
+
* Compile a policy once at config load. Patterns are case-insensitive:
|
|
45
|
+
* Windows commands are case-insensitive in reality (`Goose.exe` ≡ `goose.exe`),
|
|
46
|
+
* and for deny rules matching more broadly is the safe direction.
|
|
47
|
+
*/
|
|
48
|
+
export declare function compilePolicy(config?: PolicyConfig): CompiledPolicy;
|
|
49
|
+
/**
|
|
50
|
+
* Decide whether a command may start a session.
|
|
51
|
+
*
|
|
52
|
+
* - `allow-all`: everything passes; the lists are inert.
|
|
53
|
+
* - `denylist`: refused if any deny pattern matches.
|
|
54
|
+
* - `allowlist`: refused unless an allow pattern matches. Deny patterns are
|
|
55
|
+
* still honoured first, so a deny rule can never be silently ignored by a
|
|
56
|
+
* mode switch.
|
|
57
|
+
*/
|
|
58
|
+
export declare function evaluateCommand(policy: CompiledPolicy, command: string, args?: string[]): PolicyDecision;
|
|
59
|
+
/** Enforce the concurrent-session cap, when one is configured. There is none by default. */
|
|
60
|
+
export declare function checkSessionCapacity(policy: CompiledPolicy, liveSessionCount: number): PolicyDecision;
|
|
61
|
+
export {};
|