Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

AgentScope.ts

106 lines3.4 KB

1import * as Context from "effect/Context";
2import * as Effect from "effect/Effect";
3​
4export interface AgentScopeShape {
5 readonly wrap: (input: {
6 readonly command: string;
7 readonly args: ReadonlyArray<string>;
8 readonly name: string;
9 readonly threadId?: string | undefined;
10 readonly env?: NodeJS.ProcessEnv | undefined;
11 /** Terminals may survive restarts; agents must remain eligible for orphan cleanup. */
12 readonly survivesRestart?: boolean | undefined;
13 /**
14 * Keep a shared process running after a tool OOM when supported.
15 * Falls back to ordinary scope behavior on older systemd.
16 */
17 readonly shared?: boolean | undefined;
18 }) => Effect.Effect<AgentScopeCommand>;
19 readonly oomKilled: (threadId: string) => Effect.Effect<boolean>;
20 /**
21 * Reaps processes left after the owning agent exits, including setsid
22 * descendants outside its process group. Preserves systemd OOM diagnosis.
23 */
24 readonly stop: (unit: string) => Effect.Effect<void>;
25 /**
26 * Forgets the thread's scope. The session manager calls it before it opens
27 * a provider session, so a scope only answers for the session that launched
28 * it, even when the next session runs without one (Cursor, OpenCode).
29 */
30 readonly clear: (threadId: string) => Effect.Effect<void>;
32​
33export interface AgentScopeCommand {
34 readonly command: string;
35 readonly args: ReadonlyArray<string>;
36 /** The systemd scope the command runs in, for `stop`. */
37 readonly unit?: string | undefined;
39​
40export const AgentScope = Context.Reference<AgentScopeShape>("@t3tools/shared/AgentScope", {
41 defaultValue: () => ({
42 wrap: ({ command, args }) => Effect.succeed({ command, args }),
43 oomKilled: () => Effect.succeed(false),
44 stop: () => Effect.void,
45 clear: () => Effect.void,
46 }),
47});
48​
49export const AgentScopeThreadId = Context.Reference<string | undefined>(
50 "@t3tools/shared/AgentScope/ThreadId",
51 { defaultValue: () => undefined },
52);
53​
54export const AGENT_OOM_SCORE_ADJ = 800;
55​
56export const RAISE_OOM_SCORE_LINE = `{ echo ${AGENT_OOM_SCORE_ADJ} >/proc/self/oom_score_adj; } 2>/dev/null`;
57​
58const LAUNCH_SCRIPT = [RAISE_OOM_SCORE_LINE, 'exec "$@"'].join("\n");
59​
60// systemd-run needs XDG_RUNTIME_DIR to reach this user's manager.
61const SCOPED_LAUNCH_SCRIPT = [
62 RAISE_OOM_SCORE_LINE,
63 ': "${XDG_RUNTIME_DIR:=$1}"',
64 "export XDG_RUNTIME_DIR",
65 "shift",
66 'exec "$@"',
67].join("\n");
68​
69export interface AgentSystemdScope {
70 readonly systemdRun: string;
71 readonly slice: string;
72 readonly unit: string;
73 readonly runtimeDir: string;
74 readonly options: ReadonlyArray<string>;
76​
77/** Exec at every step preserves the agent PID, stdio, and signals; systemd-run places it and descendants in the scope. */
78export function agentScopeCommand(input: {
79 readonly command: string;
80 readonly args: ReadonlyArray<string>;
81 readonly scope?: AgentSystemdScope | undefined;
82}): AgentScopeCommand {
83 const { command, args, scope } = input;
84 if (scope === undefined) {
85 return { command: "/bin/sh", args: ["-c", LAUNCH_SCRIPT, "t3-agent", command, ...args] };
86 }
87 return {
88 command: "/bin/sh",
89 args: [
90 "-c",
91 SCOPED_LAUNCH_SCRIPT,
92 "t3-agent",
93 scope.runtimeDir,
94 scope.systemdRun,
95 "--user",
96 "--scope",
97 "--quiet",
98 `--slice=${scope.slice}`,
99 `--unit=${scope.unit}`,
100 ...scope.options,
101 "--",
102 command,
103 ...args,
104 ],
105 };