@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 131 lines 4.7 KB
Raw
1 import { Context, Deferred, Effect, Layer, Schema, type Scope } from 'effect';
2
3 /**
4 * Signals whose numbers agree on Linux and macOS, so `128 + n` is the code a shell reports for
5 * a process they end. Others (`SIGUSR1`, `SIGBUS`, …) differ between platforms.
6 */
7 const SIGNAL_NUMBERS = {
8 SIGHUP: 1,
9 SIGINT: 2,
10 SIGQUIT: 3,
11 SIGILL: 4,
12 SIGTRAP: 5,
13 SIGABRT: 6,
14 SIGFPE: 8,
15 SIGKILL: 9,
16 SIGSEGV: 11,
17 SIGPIPE: 13,
18 SIGALRM: 14,
19 SIGTERM: 15,
20 } as const;
21
22 /** A signal that can end a process, with the same number on every platform the CLI ships for. */
23 export type TerminationSignal = keyof typeof SIGNAL_NUMBERS;
24
25 export const isTerminationSignal = Schema.is(
26 Schema.Literals([
27 'SIGHUP',
28 'SIGINT',
29 'SIGQUIT',
30 'SIGILL',
31 'SIGTRAP',
32 'SIGABRT',
33 'SIGFPE',
34 'SIGKILL',
35 'SIGSEGV',
36 'SIGPIPE',
37 'SIGALRM',
38 'SIGTERM',
39 ] satisfies ReadonlyArray<TerminationSignal>)
40 );
41
42 /** The code a shell reports for a process `signal` ended: 130 for SIGINT, 143 for SIGTERM. */
43 export const signalExitCode = (signal: TerminationSignal): number => 128 + SIGNAL_NUMBERS[signal];
44
45 /** The signals the CLI's runner handles: Ctrl-C, a polite stop, and a closed terminal. */
46 export const STOP_SIGNALS = ['SIGINT', 'SIGTERM', 'SIGHUP'] as const;
47
48 export type StopSignal = (typeof STOP_SIGNALS)[number];
49
50 /** How a command stops by itself when the CLI receives a signal, instead of being interrupted. */
51 export interface CooperativeStop {
52 /** The signals the command handles. Any other signal interrupts it. */
53 readonly signals: ReadonlyArray<StopSignal>;
54 /**
55 * Runs synchronously when a second signal, or the shutdown deadline, forces the CLI to exit
56 * before the command finished stopping: the last chance to kill what the command started.
57 */
58 readonly onForce?: () => void;
59 /** Ends a forced exit by re-raising the signal (`composio run`) rather than exiting 128 + n. */
60 readonly reraiseOnForce?: boolean;
61 }
62
63 export interface ShutdownSignalShape {
64 /**
65 * Handles `stop.signals` cooperatively for the life of the current scope: the first such
66 * signal completes `requested` instead of interrupting the command. Register right after the
67 * command acquired what it has to release; a signal before that still interrupts it.
68 */
69 readonly cooperate: (stop: CooperativeStop) => Effect.Effect<void, never, Scope.Scope>;
70 /** Completes with the first signal a cooperative command took. */
71 readonly requested: Effect.Effect<StopSignal>;
72 }
73
74 /**
75 * The signal the CLI process received, for the one invocation that runs in it. The runner
76 * (`src/run-main.ts`) owns the process signal handlers and hands the signal to a command that
77 * cooperates; every other command is interrupted.
78 */
79 export class ShutdownSignal extends Context.Service<ShutdownSignal, ShutdownSignalShape>()(
80 'services/ShutdownSignal'
81 ) {
82 /** A service no runner signals: for tests and invocations without process signals. */
83 static readonly Default: Layer.Layer<ShutdownSignal> = Layer.sync(
84 ShutdownSignal,
85 () => makeShutdownController().service
86 );
87 }
88
89 /**
90 * How a listener (`composio listen`, `dev triggers listen`) stops: Ctrl-C and SIGTERM are the
91 * documented way to end it, so it stops cleanly and exits 0. A closed terminal (SIGHUP) still
92 * interrupts it and exits 129.
93 */
94 export const LISTENER_STOP: CooperativeStop = { signals: ['SIGINT', 'SIGTERM'] };
95
96 /** The runner's side of {@link ShutdownSignal}. */
97 export interface ShutdownController {
98 readonly service: ShutdownSignalShape;
99 /** Gives `signal` to a command that cooperates on it; `false` when the runner must interrupt. */
100 readonly offer: (signal: StopSignal) => boolean;
101 /** Runs every cooperating command's `onForce`; says whether the forced exit re-raises. */
102 readonly force: () => { readonly reraise: boolean };
103 }
104
105 export const makeShutdownController = (): ShutdownController => {
106 const requested = Deferred.makeUnsafe<StopSignal>();
107 const active = new Set<CooperativeStop>();
108 return {
109 service: ShutdownSignal.of({
110 cooperate: stop =>
111 Effect.acquireRelease(
112 // A fresh entry per registration, so registering one value twice is two registrations.
113 Effect.sync(() => {
114 const entry = { ...stop };
115 active.add(entry);
116 return entry;
117 }),
118 entry => Effect.sync(() => active.delete(entry))
119 ),
120 requested: Deferred.await(requested),
121 }),
122 offer: signal =>
123 [...active].some(stop => stop.signals.includes(signal)) &&
124 Deferred.doneUnsafe(requested, Effect.succeed(signal)),
125 force: () => {
126 const stops = [...active];
127 stops.forEach(stop => stop.onForce?.());
128 return { reraise: stops.some(stop => stop.reraiseOnForce === true) };
129 },
130 };
131 };