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

AgentScope.ts

102 lines3.2 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 /**
12 * Set for terminals, so a job the user detached (tmux, nohup) outlives a
13 * server restart. Agents leave it unset: the next server stops agents a
14 * killed server left running before it resumes their sessions.
15 */
16 readonly survivesRestart?: boolean | undefined;
17 }) => Effect.Effect<AgentScopeCommand>;
18 readonly oomKilled: (threadId: string) => Effect.Effect<boolean>;
19 /**
20 * Forgets the thread's scope. The session manager calls it before it opens
21 * a provider session, so a scope only answers for the session that launched
22 * it, even when the next session runs without one (Cursor, OpenCode).
23 */
24 readonly clear: (threadId: string) => Effect.Effect<void>;
26​
27export interface AgentScopeCommand {
28 readonly command: string;
29 readonly args: ReadonlyArray<string>;
31​
32export const AgentScope = Context.Reference<AgentScopeShape>("@t3tools/shared/AgentScope", {
33 defaultValue: () => ({
34 wrap: ({ command, args }) => Effect.succeed({ command, args }),
35 oomKilled: () => Effect.succeed(false),
36 clear: () => Effect.void,
37 }),
38});
39​
40export const AgentScopeThreadId = Context.Reference<string | undefined>(
41 "@t3tools/shared/AgentScope/ThreadId",
42 { defaultValue: () => undefined },
43);
44​
45export const AGENT_OOM_SCORE_ADJ = 800;
46​
47export const RAISE_OOM_SCORE_LINE = `{ echo ${AGENT_OOM_SCORE_ADJ} >/proc/self/oom_score_adj; } 2>/dev/null`;
48​
49const LAUNCH_SCRIPT = [RAISE_OOM_SCORE_LINE, 'exec "$@"'].join("\n");
50​
51// systemd-run needs XDG_RUNTIME_DIR to reach the user manager. Fill it in when
52// the agent environment does not carry it; it is the same user either way.
53const SCOPED_LAUNCH_SCRIPT = [
54 RAISE_OOM_SCORE_LINE,
55 ': "${XDG_RUNTIME_DIR:=$1}"',
56 "export XDG_RUNTIME_DIR",
57 "shift",
58 'exec "$@"',
59].join("\n");
60​
61export interface AgentSystemdScope {
62 readonly systemdRun: string;
63 readonly slice: string;
64 readonly unit: string;
65 readonly runtimeDir: string;
66 readonly options: ReadonlyArray<string>;
68​
69/**
70 * Every step `exec`s, so the spawned PID, stdio, and signals all belong to the agent itself.
71 * `systemd-run --scope` registers its own PID as the scope and then runs the command, so the
72 * agent and all of its children start inside the scope.
73 */
74export function agentScopeCommand(input: {
75 readonly command: string;
76 readonly args: ReadonlyArray<string>;
77 readonly scope?: AgentSystemdScope | undefined;
78}): AgentScopeCommand {
79 const { command, args, scope } = input;
80 if (scope === undefined) {
81 return { command: "/bin/sh", args: ["-c", LAUNCH_SCRIPT, "t3-agent", command, ...args] };
82 }
83 return {
84 command: "/bin/sh",
85 args: [
86 "-c",
87 SCOPED_LAUNCH_SCRIPT,
88 "t3-agent",
89 scope.runtimeDir,
90 scope.systemdRun,
91 "--user",
92 "--scope",
93 "--quiet",
94 `--slice=${scope.slice}`,
95 `--unit=${scope.unit}`,
96 ...scope.options,
97 "--",
98 command,
99 ...args,
100 ],
101 };