doer-agent 0.8.3 → 0.8.5

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.
@@ -70,6 +70,22 @@ export function buildDaemonMcpConfigArgs(args) {
70
70
  workspaceRootEnvName: "DOER_DAEMON_WORKSPACE_ROOT",
71
71
  });
72
72
  }
73
+ export function buildThreadsMcpConfigArgs(args) {
74
+ return buildWorkspaceMcpConfigArgs({
75
+ agentProjectDir: args.agentProjectDir,
76
+ workspaceRoot: args.workspaceRoot,
77
+ serverName: args.serverName?.trim() || "doer_threads",
78
+ distEntryRelativePath: path.join("dist", "threads-mcp-server.js"),
79
+ srcEntryRelativePath: path.join("src", "threads-mcp-server.ts"),
80
+ workspaceRootEnvName: "DOER_THREADS_WORKSPACE_ROOT",
81
+ env: {
82
+ DOER_THREADS_AGENT_ID: args.agentId,
83
+ DOER_AGENT_TOKEN: args.agentToken,
84
+ DOER_THREADS_SERVER_BASE_URL: args.serverBaseUrl,
85
+ DOER_THREADS_USER_ID: args.userId,
86
+ },
87
+ });
88
+ }
73
89
  export function buildMobileMcpConfigArgs(args) {
74
90
  return buildWorkspaceMcpConfigArgs({
75
91
  agentProjectDir: args.agentProjectDir,
@@ -88,7 +104,7 @@ export function buildMobileMcpConfigArgs(args) {
88
104
  }
89
105
  export function buildCustomMcpConfigArgs(servers) {
90
106
  const configArgs = [];
91
- const reservedNames = new Set(["doer_daemon", "doer_mobile"]);
107
+ const reservedNames = new Set(["doer_daemon", "doer_mobile", "doer_threads"]);
92
108
  const seenNames = new Set();
93
109
  for (const server of servers) {
94
110
  const serverName = server.name.trim();
@@ -18,6 +18,7 @@ export class CodexAppServerClient {
18
18
  stdoutLines = null;
19
19
  nextRequestId = 1;
20
20
  startPromise = null;
21
+ stopPromise = null;
21
22
  pending = new Map();
22
23
  constructor(options) {
23
24
  this.options = options;
@@ -37,10 +38,19 @@ export class CodexAppServerClient {
37
38
  }
38
39
  async stop() {
39
40
  const child = this.child;
40
- if (!child || child.killed) {
41
+ if (!child) {
41
42
  return;
42
43
  }
43
- child.kill("SIGTERM");
44
+ if (this.stopPromise) {
45
+ return await this.stopPromise;
46
+ }
47
+ this.stopPromise = this.stopChild(child);
48
+ try {
49
+ await this.stopPromise;
50
+ }
51
+ finally {
52
+ this.stopPromise = null;
53
+ }
44
54
  }
45
55
  async start() {
46
56
  if (this.child && !this.child.killed) {
@@ -60,9 +70,12 @@ export class CodexAppServerClient {
60
70
  async startInner() {
61
71
  this.child = spawn(process.execPath, [resolveCodexCliBinPath(), ...this.options.args], {
62
72
  cwd: this.options.cwd,
73
+ detached: process.platform !== "win32",
63
74
  env: this.options.env,
64
75
  stdio: ["pipe", "pipe", "pipe"],
65
76
  });
77
+ const childPid = this.child.pid;
78
+ const removeExitHooks = this.registerProcessExitHooks(childPid);
66
79
  this.child.stdout.setEncoding("utf8");
67
80
  this.child.stderr.setEncoding("utf8");
68
81
  this.stdoutLines = createInterface({ input: this.child.stdout });
@@ -75,6 +88,8 @@ export class CodexAppServerClient {
75
88
  });
76
89
  this.child.once("exit", (code, signal) => {
77
90
  this.options.onLog?.(`[codex-app-server] exited code=${code ?? "null"} signal=${signal ?? "null"}`);
91
+ removeExitHooks();
92
+ this.signalProcessGroup(childPid, "SIGTERM");
78
93
  this.rejectPending(new Error("Codex app-server exited"));
79
94
  this.stdoutLines?.close();
80
95
  this.stdoutLines = null;
@@ -157,4 +172,95 @@ export class CodexAppServerClient {
157
172
  pending.reject(error);
158
173
  }
159
174
  }
175
+ async stopChild(child) {
176
+ const pid = child.pid;
177
+ if (!pid) {
178
+ child.kill("SIGTERM");
179
+ return;
180
+ }
181
+ this.signalProcessGroup(pid, "SIGTERM") || child.kill("SIGTERM");
182
+ const exited = await this.waitForExit(child, 5_000);
183
+ if (!exited) {
184
+ this.options.onLog?.("[codex-app-server] forcing process group shutdown after timeout");
185
+ this.signalProcessGroup(pid, "SIGKILL") || child.kill("SIGKILL");
186
+ await this.waitForExit(child, 1_000);
187
+ }
188
+ else {
189
+ this.signalProcessGroup(pid, "SIGTERM");
190
+ }
191
+ }
192
+ registerProcessExitHooks(pid) {
193
+ if (!pid || process.platform === "win32") {
194
+ return () => { };
195
+ }
196
+ let removed = false;
197
+ const cleanup = () => {
198
+ this.signalProcessGroup(pid, "SIGTERM");
199
+ };
200
+ const signalHandlers = new Map();
201
+ const remove = () => {
202
+ if (removed) {
203
+ return;
204
+ }
205
+ removed = true;
206
+ process.off("exit", cleanup);
207
+ for (const [signal, handler] of signalHandlers) {
208
+ process.off(signal, handler);
209
+ }
210
+ };
211
+ process.once("exit", cleanup);
212
+ for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"]) {
213
+ const handler = () => {
214
+ cleanup();
215
+ remove();
216
+ try {
217
+ process.kill(process.pid, signal);
218
+ }
219
+ catch {
220
+ process.exitCode = 1;
221
+ }
222
+ };
223
+ signalHandlers.set(signal, handler);
224
+ process.once(signal, handler);
225
+ }
226
+ return remove;
227
+ }
228
+ signalProcessGroup(pid, signal) {
229
+ if (!pid || process.platform === "win32") {
230
+ return false;
231
+ }
232
+ try {
233
+ process.kill(-pid, signal);
234
+ return true;
235
+ }
236
+ catch (error) {
237
+ const code = typeof error === "object" && error !== null && "code" in error
238
+ ? String(error.code)
239
+ : "";
240
+ if (code && code !== "ESRCH") {
241
+ this.options.onLog?.(`[codex-app-server] failed to signal process group pid=${pid} signal=${signal} code=${code}`);
242
+ }
243
+ return false;
244
+ }
245
+ }
246
+ async waitForExit(child, timeoutMs) {
247
+ if (child.exitCode !== null || child.signalCode !== null) {
248
+ return true;
249
+ }
250
+ return await new Promise((resolve) => {
251
+ const timer = setTimeout(() => {
252
+ cleanup();
253
+ resolve(false);
254
+ }, timeoutMs);
255
+ const onExit = () => {
256
+ cleanup();
257
+ resolve(true);
258
+ };
259
+ const cleanup = () => {
260
+ clearTimeout(timer);
261
+ child.off("exit", onExit);
262
+ };
263
+ child.once("exit", onExit);
264
+ });
265
+ }
160
266
  }
@@ -1,5 +1,5 @@
1
1
  import { buildAgentSettingsEnvPatch, readAgentModelInstructions, resolveAgentModelInstructionsFilePath, } from "./agent-settings.js";
2
- import { buildCustomMcpConfigArgs, buildDaemonMcpConfigArgs, buildMobileMcpConfigArgs } from "./agent-codex-cli.js";
2
+ import { buildCustomMcpConfigArgs, buildDaemonMcpConfigArgs, buildMobileMcpConfigArgs, buildThreadsMcpConfigArgs } from "./agent-codex-cli.js";
3
3
  import { CodexAppServerClient } from "./codex-app-server-client.js";
4
4
  function toTomlStringLiteral(value) {
5
5
  return `"${value.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
@@ -32,6 +32,14 @@ async function buildCodexAppServerArgs(args) {
32
32
  agentProjectDir: args.agentProjectDir,
33
33
  workspaceRoot: args.workspaceRoot,
34
34
  }),
35
+ ...buildThreadsMcpConfigArgs({
36
+ agentId: args.agentId,
37
+ agentProjectDir: args.agentProjectDir,
38
+ agentToken: args.agentToken,
39
+ serverBaseUrl: args.serverBaseUrl,
40
+ userId: args.userId,
41
+ workspaceRoot: args.workspaceRoot,
42
+ }),
35
43
  ...buildMobileMcpConfigArgs({
36
44
  agentId: args.agentId,
37
45
  agentProjectDir: args.agentProjectDir,
@@ -0,0 +1,170 @@
1
+ import path from "node:path";
2
+ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
3
+ import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
4
+ import * as z from "zod/v4";
5
+ function parseWorkspaceRoot(argv) {
6
+ const flagIndex = argv.findIndex((token) => token === "--workspace-root");
7
+ const flagValue = flagIndex >= 0 ? argv[flagIndex + 1] : "";
8
+ const envValue = process.env.DOER_THREADS_WORKSPACE_ROOT?.trim() || process.env.WORKSPACE?.trim() || process.cwd();
9
+ return path.resolve((flagValue || envValue || process.cwd()).trim());
10
+ }
11
+ function formatJson(value) {
12
+ return JSON.stringify(value, null, 2);
13
+ }
14
+ function optionalEnv(name) {
15
+ return process.env[name]?.trim() || "";
16
+ }
17
+ function getThreadApiConfig() {
18
+ const agentId = optionalEnv("DOER_THREADS_AGENT_ID");
19
+ const agentToken = optionalEnv("DOER_AGENT_TOKEN");
20
+ const serverBaseUrl = optionalEnv("DOER_THREADS_SERVER_BASE_URL").replace(/\/$/, "");
21
+ const userId = optionalEnv("DOER_THREADS_USER_ID");
22
+ const missing = [
23
+ agentId ? null : "DOER_THREADS_AGENT_ID",
24
+ agentToken ? null : "DOER_AGENT_TOKEN",
25
+ serverBaseUrl ? null : "DOER_THREADS_SERVER_BASE_URL",
26
+ userId ? null : "DOER_THREADS_USER_ID",
27
+ ].filter((item) => Boolean(item));
28
+ if (missing.length > 0) {
29
+ throw new Error(`thread tools are unavailable; missing ${missing.join(", ")}`);
30
+ }
31
+ return { agentId, agentToken, serverBaseUrl, userId };
32
+ }
33
+ function codexThreadPath(config, method) {
34
+ return `/api/users/${encodeURIComponent(config.userId)}/agents/${encodeURIComponent(config.agentId)}/codex/${method}`;
35
+ }
36
+ async function postDoerJson(pathValue, body, timeoutMs) {
37
+ const config = getThreadApiConfig();
38
+ const response = await fetch(`${config.serverBaseUrl}${pathValue}`, {
39
+ method: "POST",
40
+ headers: {
41
+ Authorization: `Bearer ${config.agentToken}`,
42
+ Accept: "application/json",
43
+ "Content-Type": "application/json",
44
+ ...(timeoutMs ? { "x-doer-rpc-timeout-ms": String(timeoutMs) } : {}),
45
+ },
46
+ body: JSON.stringify(body),
47
+ });
48
+ const data = await response.json().catch(() => ({}));
49
+ if (!response.ok) {
50
+ throw new Error(typeof data.error === "string" ? data.error : `Doer server returned ${response.status}`);
51
+ }
52
+ return data;
53
+ }
54
+ function jsonToolResult(result) {
55
+ return {
56
+ content: [{ type: "text", text: formatJson(result) }],
57
+ structuredContent: result && typeof result === "object" && !Array.isArray(result)
58
+ ? result
59
+ : { result },
60
+ };
61
+ }
62
+ function threadListParams(args) {
63
+ const limit = Number.isFinite(args.limit) && args.limit
64
+ ? Math.min(100, Math.max(1, Math.trunc(args.limit)))
65
+ : 50;
66
+ return {
67
+ cursor: args.cursor?.trim() || null,
68
+ limit,
69
+ sortKey: "updated_at",
70
+ sortDirection: "desc",
71
+ sourceKinds: [
72
+ "cli",
73
+ "vscode",
74
+ "exec",
75
+ "appServer",
76
+ "subAgent",
77
+ "subAgentReview",
78
+ "subAgentCompact",
79
+ "subAgentThreadSpawn",
80
+ "subAgentOther",
81
+ "unknown",
82
+ ],
83
+ archived: args.archived ?? false,
84
+ searchTerm: args.searchTerm?.trim() || null,
85
+ };
86
+ }
87
+ async function archiveThread(threadId) {
88
+ const config = getThreadApiConfig();
89
+ return postDoerJson(codexThreadPath(config, "thread/archive"), { threadId: threadId.trim() }, 180_000);
90
+ }
91
+ async function main() {
92
+ parseWorkspaceRoot(process.argv.slice(2));
93
+ const server = new McpServer({
94
+ name: "doer-threads",
95
+ version: "0.1.0",
96
+ }, {
97
+ capabilities: {
98
+ tools: {},
99
+ },
100
+ instructions: "Start, list, read, close, and archive Codex threads for the current Doer agent.",
101
+ });
102
+ server.registerTool("threads_list", {
103
+ description: "List Codex threads for this Doer agent using the same API as the Doer thread list.",
104
+ inputSchema: {
105
+ archived: z.boolean().optional().describe("Whether to list archived threads. Defaults to false."),
106
+ cursor: z.string().optional().describe("Optional pagination cursor returned by the thread list API."),
107
+ limit: z.number().int().min(1).max(100).optional().describe("Maximum number of threads to return. Defaults to 50."),
108
+ searchTerm: z.string().optional().describe("Optional text search term."),
109
+ },
110
+ }, async ({ archived, cursor, limit, searchTerm }) => {
111
+ const config = getThreadApiConfig();
112
+ const result = await postDoerJson(codexThreadPath(config, "thread/list"), threadListParams({ archived, cursor, limit, searchTerm }), 180_000);
113
+ return jsonToolResult(result);
114
+ });
115
+ server.registerTool("threads_start", {
116
+ description: "Create a new Codex thread for this Doer agent and start its first turn.",
117
+ inputSchema: {
118
+ prompt: z.string().min(1).describe("User prompt for the first turn in the new thread."),
119
+ },
120
+ }, async ({ prompt }) => {
121
+ const config = getThreadApiConfig();
122
+ const result = await postDoerJson(codexThreadPath(config, "thread/send"), { prompt }, 180_000);
123
+ return jsonToolResult(result);
124
+ });
125
+ server.registerTool("threads_read", {
126
+ description: "Read a Codex thread and its recent turn contents for this Doer agent.",
127
+ inputSchema: {
128
+ threadId: z.string().min(1).describe("Codex thread id to read."),
129
+ cursor: z.string().optional().describe("Optional pagination cursor for turns."),
130
+ limit: z.number().int().min(1).max(100).optional().describe("Maximum number of turns to return. Defaults to 50."),
131
+ },
132
+ }, async ({ threadId, cursor, limit }) => {
133
+ const config = getThreadApiConfig();
134
+ const normalizedThreadId = threadId.trim();
135
+ const turnsLimit = Number.isFinite(limit) && limit ? Math.min(100, Math.max(1, Math.trunc(limit))) : 50;
136
+ const [thread, turns] = await Promise.all([
137
+ postDoerJson(codexThreadPath(config, "thread/read"), { threadId: normalizedThreadId }, 180_000),
138
+ postDoerJson(codexThreadPath(config, "thread/turns/list"), {
139
+ threadId: normalizedThreadId,
140
+ cursor: cursor?.trim() || null,
141
+ limit: turnsLimit,
142
+ sortDirection: "desc",
143
+ }, 180_000),
144
+ ]);
145
+ return jsonToolResult({ thread, turns });
146
+ });
147
+ server.registerTool("threads_close", {
148
+ description: "Close a Codex thread for this Doer agent by archiving it with the same API as Doer thread delete.",
149
+ inputSchema: {
150
+ threadId: z.string().min(1).describe("Codex thread id to close."),
151
+ },
152
+ }, async ({ threadId }) => {
153
+ return jsonToolResult(await archiveThread(threadId) ?? { ok: true });
154
+ });
155
+ server.registerTool("threads_archive", {
156
+ description: "Archive a Codex thread for this Doer agent using the same API as Doer thread delete.",
157
+ inputSchema: {
158
+ threadId: z.string().min(1).describe("Codex thread id to archive."),
159
+ },
160
+ }, async ({ threadId }) => {
161
+ return jsonToolResult(await archiveThread(threadId) ?? { ok: true });
162
+ });
163
+ const transport = new StdioServerTransport();
164
+ await server.connect(transport);
165
+ }
166
+ main().catch((error) => {
167
+ const message = error instanceof Error ? error.stack || error.message : String(error);
168
+ process.stderr.write(`${message}\n`);
169
+ process.exit(1);
170
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "doer-agent",
3
- "version": "0.8.3",
3
+ "version": "0.8.5",
4
4
  "description": "Reverse-polling agent runtime for doer",
5
5
  "type": "module",
6
6
  "main": "dist/agent.js",