talon-agent 1.8.1 → 1.9.1
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/README.md +50 -37
- package/package.json +1 -2
- package/prompts/mempalace.md +3 -0
- package/src/__tests__/disallowed-tools.test.ts +64 -0
- package/src/__tests__/handlers-stream.test.ts +0 -5
- package/src/__tests__/handlers.test.ts +6 -12
- package/src/__tests__/mcp-launcher-functional.test.ts +334 -0
- package/src/__tests__/mcp-launcher.test.ts +139 -0
- package/src/__tests__/mempalace-plugin.test.ts +56 -1
- package/src/__tests__/plugin.test.ts +8 -0
- package/src/backend/claude-sdk/options.ts +5 -4
- package/src/core/constants.ts +5 -0
- package/src/core/dream.ts +1 -1
- package/src/core/plugin.ts +13 -7
- package/src/frontend/telegram/handlers.ts +4 -12
- package/src/plugins/mempalace/index.ts +34 -10
- package/src/util/config.ts +8 -0
- package/src/util/log.ts +2 -7
- package/src/util/mcp-launcher.mjs +72 -0
- package/src/util/mcp-launcher.ts +58 -0
- package/src/__tests__/prompt-builder-extended.test.ts +0 -296
- package/src/__tests__/prompt-builder.test.ts +0 -106
- package/src/core/prompt-builder.ts +0 -40
package/README.md
CHANGED
|
@@ -12,15 +12,15 @@ Multi-platform agentic AI harness powered by Claude. Runs on **Telegram**, **Tea
|
|
|
12
12
|
|
|
13
13
|
## Features
|
|
14
14
|
|
|
15
|
-
|
|
|
16
|
-
|
|
17
|
-
| **Multi-frontend**
|
|
18
|
-
| **Claude Agent SDK**
|
|
19
|
-
| **MCP tools**
|
|
20
|
-
| **Plugins**
|
|
21
|
-
| **Background agents** | Heartbeat (periodic maintenance) and Dream (memory consolidation + diary)
|
|
22
|
-
| **Per-chat settings** | Model, effort level, and pulse toggle per conversation via inline keyboard
|
|
23
|
-
| **Model registry**
|
|
15
|
+
| | |
|
|
16
|
+
| --------------------- | ------------------------------------------------------------------------------------------------------- |
|
|
17
|
+
| **Multi-frontend** | Telegram (Grammy + GramJS userbot), Microsoft Teams (Bot Framework), Terminal with live tool visibility |
|
|
18
|
+
| **Claude Agent SDK** | Streaming responses, extended thinking, adaptive effort, 1M token context, dynamic model discovery |
|
|
19
|
+
| **MCP tools** | Messaging, media, history, search, web fetch, cron jobs, stickers, file system, admin controls |
|
|
20
|
+
| **Plugins** | Hot-reloadable plugin system. Built-in: GitHub, MemPalace, Playwright, Brave Search |
|
|
21
|
+
| **Background agents** | Heartbeat (periodic maintenance) and Dream (memory consolidation + diary) |
|
|
22
|
+
| **Per-chat settings** | Model, effort level, and pulse toggle per conversation via inline keyboard |
|
|
23
|
+
| **Model registry** | Models discovered from the SDK at startup --- new models appear in all pickers automatically |
|
|
24
24
|
|
|
25
25
|
---
|
|
26
26
|
|
|
@@ -39,8 +39,10 @@ npx talon chat # terminal chat mode
|
|
|
39
39
|
```
|
|
40
40
|
|
|
41
41
|
**Prerequisites:**
|
|
42
|
+
|
|
42
43
|
- [Node.js 22+](https://nodejs.org/)
|
|
43
44
|
- [Claude Code](https://docs.anthropic.com/en/docs/claude-code) installed and authenticated (`claude` CLI on PATH)
|
|
45
|
+
- Talon runs from a normal source or package install; standalone compiled binaries are not supported.
|
|
44
46
|
|
|
45
47
|
---
|
|
46
48
|
|
|
@@ -161,9 +163,7 @@ Plugins add MCP tools and gateway actions without modifying core code. SOLID int
|
|
|
161
163
|
|
|
162
164
|
```json
|
|
163
165
|
{
|
|
164
|
-
"plugins": [
|
|
165
|
-
{ "path": "/path/to/my-plugin", "config": { "apiKey": "..." } }
|
|
166
|
-
]
|
|
166
|
+
"plugins": [{ "path": "/path/to/my-plugin", "config": { "apiKey": "..." } }]
|
|
167
167
|
}
|
|
168
168
|
```
|
|
169
169
|
|
|
@@ -172,12 +172,24 @@ export default {
|
|
|
172
172
|
name: "my-plugin",
|
|
173
173
|
version: "1.0.0",
|
|
174
174
|
mcpServerPath: resolve(import.meta.dirname, "tools.ts"),
|
|
175
|
-
validateConfig(config) {
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
175
|
+
validateConfig(config) {
|
|
176
|
+
/* return errors or undefined */
|
|
177
|
+
},
|
|
178
|
+
getEnvVars(config) {
|
|
179
|
+
return { MY_KEY: config.apiKey };
|
|
180
|
+
},
|
|
181
|
+
handleAction(body, chatId) {
|
|
182
|
+
/* gateway action handler */
|
|
183
|
+
},
|
|
184
|
+
getSystemPromptAddition(config) {
|
|
185
|
+
return "## My Plugin\n...";
|
|
186
|
+
},
|
|
187
|
+
init(config) {
|
|
188
|
+
/* one-time setup */
|
|
189
|
+
},
|
|
190
|
+
destroy() {
|
|
191
|
+
/* cleanup */
|
|
192
|
+
},
|
|
181
193
|
};
|
|
182
194
|
```
|
|
183
195
|
|
|
@@ -204,25 +216,25 @@ talon doctor Validate environment and dependencies
|
|
|
204
216
|
|
|
205
217
|
Config file: `~/.talon/config.json`
|
|
206
218
|
|
|
207
|
-
| Field
|
|
208
|
-
|
|
209
|
-
| `frontend`
|
|
210
|
-
| `backend`
|
|
211
|
-
| `botToken`
|
|
212
|
-
| `model`
|
|
213
|
-
| `concurrency`
|
|
214
|
-
| `pulse`
|
|
215
|
-
| `heartbeat`
|
|
216
|
-
| `heartbeatIntervalMinutes` | `60`
|
|
217
|
-
| `braveApiKey`
|
|
218
|
-
| `timezone`
|
|
219
|
-
| `plugins`
|
|
220
|
-
| `adminUserId`
|
|
221
|
-
| `allowedUsers`
|
|
222
|
-
| `apiId` / `apiHash`
|
|
223
|
-
| `github`
|
|
224
|
-
| `mempalace`
|
|
225
|
-
| `playwright`
|
|
219
|
+
| Field | Default | Description |
|
|
220
|
+
| -------------------------- | ------------ | ------------------------------------------------------------------- |
|
|
221
|
+
| `frontend` | `"telegram"` | `"telegram"`, `"terminal"`, `"teams"`, or an array |
|
|
222
|
+
| `backend` | `"claude"` | `"claude"` or `"opencode"` |
|
|
223
|
+
| `botToken` | --- | Telegram bot token |
|
|
224
|
+
| `model` | `"default"` | Default Claude model. Legacy `claude-*` aliases are still accepted. |
|
|
225
|
+
| `concurrency` | `1` | Max concurrent AI queries (1--20) |
|
|
226
|
+
| `pulse` | `true` | Periodic group engagement |
|
|
227
|
+
| `heartbeat` | `false` | Background maintenance agent |
|
|
228
|
+
| `heartbeatIntervalMinutes` | `60` | Heartbeat interval |
|
|
229
|
+
| `braveApiKey` | --- | Brave Search API key |
|
|
230
|
+
| `timezone` | --- | IANA timezone (e.g. `"Europe/London"`) |
|
|
231
|
+
| `plugins` | `[]` | External plugin packages |
|
|
232
|
+
| `adminUserId` | --- | Telegram user ID for `/admin` commands |
|
|
233
|
+
| `allowedUsers` | --- | Whitelist of Telegram user IDs |
|
|
234
|
+
| `apiId` / `apiHash` | --- | Telegram API credentials for full message history |
|
|
235
|
+
| `github` | --- | GitHub plugin config (see above) |
|
|
236
|
+
| `mempalace` | --- | MemPalace plugin config (see above) |
|
|
237
|
+
| `playwright` | --- | Playwright plugin config (see above) |
|
|
226
238
|
|
|
227
239
|
---
|
|
228
240
|
|
|
@@ -241,6 +253,7 @@ Commands: `/model`, `/effort`, `/reset`, `/status`, `/help`
|
|
|
241
253
|
## Production
|
|
242
254
|
|
|
243
255
|
**Docker:**
|
|
256
|
+
|
|
244
257
|
```bash
|
|
245
258
|
docker compose up -d
|
|
246
259
|
```
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "talon-agent",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.1",
|
|
4
4
|
"description": "Multi-frontend AI agent with full tool access, streaming, cron jobs, and plugin system",
|
|
5
5
|
"author": "Dylan Neve",
|
|
6
6
|
"license": "MIT",
|
|
@@ -36,7 +36,6 @@
|
|
|
36
36
|
"tsconfig.json"
|
|
37
37
|
],
|
|
38
38
|
"scripts": {
|
|
39
|
-
"build:binary": "bun build --compile --minify src/index.ts --outfile talon-bun",
|
|
40
39
|
"start": "tsx src/index.ts",
|
|
41
40
|
"cli": "tsx src/cli.ts",
|
|
42
41
|
"setup": "tsx src/cli.ts setup",
|
package/prompts/mempalace.md
CHANGED
|
@@ -53,5 +53,8 @@ You have access to a local memory palace via MCP tools. The palace stores verbat
|
|
|
53
53
|
- The knowledge graph stores typed relationships with **time windows**. It knows WHEN things were true.
|
|
54
54
|
- Use `mempalace_check_duplicate` before storing new content to avoid clutter.
|
|
55
55
|
- Diary entries accumulate across sessions. Write them to build continuity of self.
|
|
56
|
+
- Entity detection runs per-language; results include `created_at` timestamps you can surface when the user asks "when did I last…".
|
|
56
57
|
|
|
57
58
|
### Palace location: `{{palacePath}}`
|
|
59
|
+
|
|
60
|
+
### Entity-detection languages: `{{entityLanguages}}`
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DISALLOWED_TOOLS_BACKGROUND,
|
|
5
|
+
DISALLOWED_TOOLS_CORE,
|
|
6
|
+
} from "../core/constants.js";
|
|
7
|
+
import { DISALLOWED_TOOLS_CHAT } from "../backend/claude-sdk/constants.js";
|
|
8
|
+
|
|
9
|
+
describe("disallowed tool lists", () => {
|
|
10
|
+
describe("DISALLOWED_TOOLS_CORE", () => {
|
|
11
|
+
it("blocks interactive/planning tools that are nonsensical in headless contexts", () => {
|
|
12
|
+
const expected = [
|
|
13
|
+
"EnterPlanMode",
|
|
14
|
+
"ExitPlanMode",
|
|
15
|
+
"EnterWorktree",
|
|
16
|
+
"ExitWorktree",
|
|
17
|
+
"TodoWrite",
|
|
18
|
+
"TodoRead",
|
|
19
|
+
"TaskCreate",
|
|
20
|
+
"TaskUpdate",
|
|
21
|
+
"TaskGet",
|
|
22
|
+
"TaskList",
|
|
23
|
+
"TaskOutput",
|
|
24
|
+
"TaskStop",
|
|
25
|
+
"AskUserQuestion",
|
|
26
|
+
];
|
|
27
|
+
for (const tool of expected) {
|
|
28
|
+
expect(DISALLOWED_TOOLS_CORE).toContain(tool);
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("blocks ScheduleWakeup — /loop-skill-only tool that wedges the dispatcher when called outside /loop mode", () => {
|
|
33
|
+
// Confirmed root cause of a 35-minute hang on 2026-04-27.
|
|
34
|
+
// ScheduleWakeup registers a wakeup the runtime never fires, so the
|
|
35
|
+
// chat lock is held indefinitely until manual restart.
|
|
36
|
+
expect(DISALLOWED_TOOLS_CORE).toContain("ScheduleWakeup");
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe("DISALLOWED_TOOLS_BACKGROUND", () => {
|
|
41
|
+
it("inherits everything from CORE", () => {
|
|
42
|
+
for (const tool of DISALLOWED_TOOLS_CORE) {
|
|
43
|
+
expect(DISALLOWED_TOOLS_BACKGROUND).toContain(tool);
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("additionally blocks Agent (no nested agents in dream/heartbeat)", () => {
|
|
48
|
+
expect(DISALLOWED_TOOLS_BACKGROUND).toContain("Agent");
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe("DISALLOWED_TOOLS_CHAT", () => {
|
|
53
|
+
it("inherits everything from CORE", () => {
|
|
54
|
+
for (const tool of DISALLOWED_TOOLS_CORE) {
|
|
55
|
+
expect(DISALLOWED_TOOLS_CHAT).toContain(tool);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("additionally blocks Claude's built-in web tools (replaced by Brave MCP)", () => {
|
|
60
|
+
expect(DISALLOWED_TOOLS_CHAT).toContain("WebSearch");
|
|
61
|
+
expect(DISALLOWED_TOOLS_CHAT).toContain("WebFetch");
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
@@ -29,11 +29,6 @@ vi.mock("../core/errors.js", () => ({
|
|
|
29
29
|
TalonError: class TalonError extends Error {},
|
|
30
30
|
}));
|
|
31
31
|
|
|
32
|
-
vi.mock("../core/prompt-builder.js", () => ({
|
|
33
|
-
enrichDMPrompt: vi.fn((p: string) => p),
|
|
34
|
-
enrichGroupPrompt: vi.fn((p: string) => p),
|
|
35
|
-
}));
|
|
36
|
-
|
|
37
32
|
vi.mock("../storage/daily-log.js", () => ({
|
|
38
33
|
appendDailyLog: vi.fn(),
|
|
39
34
|
appendDailyLogResponse: vi.fn(),
|
|
@@ -5,10 +5,6 @@ const executeMock = vi.hoisted(() => vi.fn());
|
|
|
5
5
|
vi.mock("../core/dispatcher.js", () => ({
|
|
6
6
|
execute: executeMock,
|
|
7
7
|
}));
|
|
8
|
-
vi.mock("../core/prompt-builder.js", () => ({
|
|
9
|
-
enrichDMPrompt: vi.fn((p: string) => p),
|
|
10
|
-
enrichGroupPrompt: vi.fn((p: string) => p),
|
|
11
|
-
}));
|
|
12
8
|
vi.mock("../storage/daily-log.js", () => ({
|
|
13
9
|
appendDailyLog: vi.fn(),
|
|
14
10
|
appendDailyLogResponse: vi.fn(),
|
|
@@ -2641,11 +2637,8 @@ describe("handleStickerMessage — video sticker branch (L835 TRUE)", () => {
|
|
|
2641
2637
|
}, 3000);
|
|
2642
2638
|
});
|
|
2643
2639
|
|
|
2644
|
-
describe("processAndReply — group message without senderId
|
|
2645
|
-
it("
|
|
2646
|
-
const { enrichGroupPrompt } = await import("../core/prompt-builder.js");
|
|
2647
|
-
(enrichGroupPrompt as ReturnType<typeof vi.fn>).mockClear();
|
|
2648
|
-
|
|
2640
|
+
describe("processAndReply — group message without senderId", () => {
|
|
2641
|
+
it("processes anonymous group messages without mutating the prompt", async () => {
|
|
2649
2642
|
executeMock.mockResolvedValueOnce({
|
|
2650
2643
|
text: "",
|
|
2651
2644
|
durationMs: 10,
|
|
@@ -2672,13 +2665,14 @@ describe("processAndReply — group message without senderId (L552 FALSE branch)
|
|
|
2672
2665
|
await handleTextMessage(ctx, mockBot, mockConfig);
|
|
2673
2666
|
await new Promise((r) => setTimeout(r, 700));
|
|
2674
2667
|
|
|
2675
|
-
//
|
|
2676
|
-
expect(enrichGroupPrompt).not.toHaveBeenCalled();
|
|
2677
|
-
// But message was still processed
|
|
2668
|
+
// Message was still processed, and the prompt is passed through verbatim.
|
|
2678
2669
|
const calls = executeMock.mock.calls
|
|
2679
2670
|
.slice(before)
|
|
2680
2671
|
.filter((c) => (c[0] as { chatId: string }).chatId === String(chatId));
|
|
2681
2672
|
expect(calls.length).toBe(1);
|
|
2673
|
+
expect((calls[0][0] as { prompt: string }).prompt).toBe(
|
|
2674
|
+
"@testbot anonymous message",
|
|
2675
|
+
);
|
|
2682
2676
|
}, 3000);
|
|
2683
2677
|
});
|
|
2684
2678
|
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Functional tests for the MCP launcher supervisor.
|
|
3
|
+
*
|
|
4
|
+
* Each test targets a distinct failure mode that would produce an orphaned
|
|
5
|
+
* MCP subprocess in production. All spawn real Node subprocesses and tear
|
|
6
|
+
* them down in afterEach — no mocks, no shortcuts. If these pass on Linux
|
|
7
|
+
* and macOS, the "launcher-wrapped spawns never orphan" claim holds.
|
|
8
|
+
*
|
|
9
|
+
* Cases kept:
|
|
10
|
+
* 1. SIGKILL of parent at scale (headline bug from PR #67).
|
|
11
|
+
* 2. Graceful stdin-close shutdown (normal Talon exit).
|
|
12
|
+
* 3. Stubborn child that ignores SIGTERM (validates SIGKILL fallback).
|
|
13
|
+
* 4. Supervised child exits on its own (launcher doesn't hang).
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
17
|
+
import { spawn, type ChildProcess } from "node:child_process";
|
|
18
|
+
import { mkdtempSync, writeFileSync, rmSync } from "node:fs";
|
|
19
|
+
import { tmpdir } from "node:os";
|
|
20
|
+
import { join, resolve, dirname } from "node:path";
|
|
21
|
+
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
22
|
+
|
|
23
|
+
const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
|
|
24
|
+
const LAUNCHER_MODULE = pathToFileURL(
|
|
25
|
+
resolve(REPO_ROOT, "src/util/mcp-launcher.ts"),
|
|
26
|
+
).href;
|
|
27
|
+
const TSX_IMPORT = pathToFileURL(
|
|
28
|
+
resolve(REPO_ROOT, "node_modules/tsx/dist/esm/index.mjs"),
|
|
29
|
+
).href;
|
|
30
|
+
const FUNCTIONAL_TIMEOUT_MS = 30_000;
|
|
31
|
+
|
|
32
|
+
// ── Helpers ────────────────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
function pidAlive(pid: number): boolean {
|
|
35
|
+
try {
|
|
36
|
+
process.kill(pid, 0);
|
|
37
|
+
return true;
|
|
38
|
+
} catch (err) {
|
|
39
|
+
const code = (err as NodeJS.ErrnoException).code;
|
|
40
|
+
if (code === "ESRCH") return false;
|
|
41
|
+
// EPERM: exists but unreachable. Count as alive.
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function waitForPidGone(
|
|
47
|
+
pid: number,
|
|
48
|
+
timeoutMs: number,
|
|
49
|
+
): Promise<boolean> {
|
|
50
|
+
const deadline = Date.now() + timeoutMs;
|
|
51
|
+
while (Date.now() < deadline) {
|
|
52
|
+
if (!pidAlive(pid)) return true;
|
|
53
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
54
|
+
}
|
|
55
|
+
return !pidAlive(pid);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function assertAllGone(pids: number[], timeoutMs: number): Promise<void> {
|
|
59
|
+
const stuck: number[] = [];
|
|
60
|
+
for (const pid of pids) {
|
|
61
|
+
if (!(await waitForPidGone(pid, timeoutMs))) stuck.push(pid);
|
|
62
|
+
}
|
|
63
|
+
if (stuck.length > 0) {
|
|
64
|
+
// Clean up the leak so it doesn't poison sibling tests.
|
|
65
|
+
for (const pid of stuck) {
|
|
66
|
+
try {
|
|
67
|
+
process.kill(pid, "SIGKILL");
|
|
68
|
+
} catch {
|
|
69
|
+
/* ok */
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw new Error(`orphaned pids after teardown: ${stuck.join(", ")}`);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Read the first stdout line matching `match`, with a timeout. */
|
|
77
|
+
async function readMarker(
|
|
78
|
+
child: ChildProcess,
|
|
79
|
+
match: RegExp,
|
|
80
|
+
timeoutMs: number,
|
|
81
|
+
label: string,
|
|
82
|
+
): Promise<RegExpMatchArray> {
|
|
83
|
+
return new Promise((resolve, reject) => {
|
|
84
|
+
const timer = setTimeout(
|
|
85
|
+
() => reject(new Error(`timeout waiting for ${label}`)),
|
|
86
|
+
timeoutMs,
|
|
87
|
+
);
|
|
88
|
+
let buf = "";
|
|
89
|
+
const onData = (d: Buffer) => {
|
|
90
|
+
buf += d.toString();
|
|
91
|
+
const m = buf.match(match);
|
|
92
|
+
if (m) {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
child.stdout!.off("data", onData);
|
|
95
|
+
resolve(m);
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
child.stdout!.on("data", onData);
|
|
99
|
+
child.once("exit", (code) => {
|
|
100
|
+
clearTimeout(timer);
|
|
101
|
+
reject(new Error(`process exited before ${label} (code=${code})`));
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
type IdlerOpts = {
|
|
107
|
+
name?: string;
|
|
108
|
+
/** If false, the idler keeps running even after its stdin closes. */
|
|
109
|
+
exitOnStdinClose?: boolean;
|
|
110
|
+
/** If true, the idler ignores SIGTERM (forces SIGKILL cleanup path). */
|
|
111
|
+
ignoreSigterm?: boolean;
|
|
112
|
+
/** If set, the idler exits on its own this many ms after starting. */
|
|
113
|
+
selfExitAfterMs?: number;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
function writeIdler(dir: string, opts: IdlerOpts = {}): string {
|
|
117
|
+
const path = join(dir, opts.name ?? "idler.mjs");
|
|
118
|
+
const exitOnStdin = opts.exitOnStdinClose !== false;
|
|
119
|
+
const ignoreTerm = opts.ignoreSigterm === true;
|
|
120
|
+
const selfExit = opts.selfExitAfterMs;
|
|
121
|
+
writeFileSync(
|
|
122
|
+
path,
|
|
123
|
+
`
|
|
124
|
+
process.stderr.write("IDLER_PID=" + process.pid + "\\n");
|
|
125
|
+
${exitOnStdin ? 'process.stdin.on("end", () => process.exit(0));' : ""}
|
|
126
|
+
process.on("SIGTERM", () => { ${
|
|
127
|
+
ignoreTerm ? "/* stubborn: ignore */" : "process.exit(0);"
|
|
128
|
+
} });
|
|
129
|
+
process.on("SIGINT", () => process.exit(0));
|
|
130
|
+
process.stdin.resume();
|
|
131
|
+
${selfExit !== undefined ? `setTimeout(() => process.exit(0), ${selfExit});` : ""}
|
|
132
|
+
setInterval(() => {}, 1 << 30);
|
|
133
|
+
`,
|
|
134
|
+
);
|
|
135
|
+
return path;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type HarnessResult = { harness: ChildProcess; pids: number[] };
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Spawn a harness that uses the real `wrapMcpServer()` to supervise
|
|
142
|
+
* `count` idlers. Resolves once every idler has reported its PID, returning
|
|
143
|
+
* [launcher PIDs..., idler PIDs...] (length = count * 2).
|
|
144
|
+
*/
|
|
145
|
+
async function spawnHarness(opts: {
|
|
146
|
+
workDir: string;
|
|
147
|
+
count: number;
|
|
148
|
+
idlerPath: string;
|
|
149
|
+
}): Promise<HarnessResult> {
|
|
150
|
+
const { workDir, count, idlerPath } = opts;
|
|
151
|
+
const harnessPath = join(workDir, "harness.mjs");
|
|
152
|
+
writeFileSync(
|
|
153
|
+
harnessPath,
|
|
154
|
+
`
|
|
155
|
+
import { spawn } from "node:child_process";
|
|
156
|
+
import { wrapMcpServer } from ${JSON.stringify(LAUNCHER_MODULE)};
|
|
157
|
+
|
|
158
|
+
const launchers = [];
|
|
159
|
+
const idlers = [];
|
|
160
|
+
const TARGET = ${count};
|
|
161
|
+
|
|
162
|
+
for (let i = 0; i < TARGET; i++) {
|
|
163
|
+
const cfg = wrapMcpServer({
|
|
164
|
+
command: "node",
|
|
165
|
+
args: [${JSON.stringify(idlerPath)}],
|
|
166
|
+
env: {},
|
|
167
|
+
});
|
|
168
|
+
const c = spawn(cfg.command, cfg.args, {
|
|
169
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
170
|
+
env: { ...process.env, ...cfg.env },
|
|
171
|
+
});
|
|
172
|
+
launchers.push(c);
|
|
173
|
+
|
|
174
|
+
let buf = "";
|
|
175
|
+
c.stderr.on("data", (d) => {
|
|
176
|
+
buf += d.toString();
|
|
177
|
+
const m = buf.match(/IDLER_PID=(\\d+)/);
|
|
178
|
+
if (m) {
|
|
179
|
+
idlers.push(parseInt(m[1], 10));
|
|
180
|
+
buf = buf.replace(/IDLER_PID=\\d+\\n?/, "");
|
|
181
|
+
if (idlers.length === TARGET) {
|
|
182
|
+
process.stdout.write(
|
|
183
|
+
"PIDS=" + launchers.map((c) => c.pid).concat(idlers).join(",") + "\\n",
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
process.stdin.on("end", () => process.exit(0));
|
|
191
|
+
process.stdin.resume();
|
|
192
|
+
`,
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const harness = spawn("node", ["--import", TSX_IMPORT, harnessPath], {
|
|
196
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
197
|
+
env: { ...process.env, HOME: workDir },
|
|
198
|
+
});
|
|
199
|
+
const marker = await readMarker(
|
|
200
|
+
harness,
|
|
201
|
+
/PIDS=([\d,]+)/,
|
|
202
|
+
15_000,
|
|
203
|
+
"harness PID marker",
|
|
204
|
+
);
|
|
205
|
+
const pids = marker[1].split(",").map((s) => parseInt(s, 10));
|
|
206
|
+
if (pids.length !== count * 2) {
|
|
207
|
+
throw new Error(
|
|
208
|
+
`harness reported ${pids.length} pids, expected ${count * 2}`,
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
return { harness, pids };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ── Tests ──────────────────────────────────────────────────────────────────
|
|
215
|
+
|
|
216
|
+
describe("launcher functional: no orphaned MCP processes", () => {
|
|
217
|
+
let workDir: string;
|
|
218
|
+
const cleanup: Array<() => void> = [];
|
|
219
|
+
|
|
220
|
+
beforeEach(() => {
|
|
221
|
+
workDir = mkdtempSync(join(tmpdir(), "talon-launcher-fn-"));
|
|
222
|
+
process.env.HOME = workDir; // paths.ts reads homedir() → this
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
afterEach(() => {
|
|
226
|
+
for (const fn of cleanup.splice(0)) {
|
|
227
|
+
try {
|
|
228
|
+
fn();
|
|
229
|
+
} catch {
|
|
230
|
+
/* ok */
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
try {
|
|
234
|
+
rmSync(workDir, { recursive: true, force: true });
|
|
235
|
+
} catch {
|
|
236
|
+
/* ok */
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
function track(child: ChildProcess): void {
|
|
241
|
+
cleanup.push(() => {
|
|
242
|
+
if (!child.killed) {
|
|
243
|
+
try {
|
|
244
|
+
child.kill("SIGKILL");
|
|
245
|
+
} catch {
|
|
246
|
+
/* ok */
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
it(
|
|
253
|
+
"SIGKILL of parent cleans up every descendant (10 wrapped children)",
|
|
254
|
+
async () => {
|
|
255
|
+
const idler = writeIdler(workDir);
|
|
256
|
+
const { harness, pids } = await spawnHarness({
|
|
257
|
+
workDir,
|
|
258
|
+
count: 10,
|
|
259
|
+
idlerPath: idler,
|
|
260
|
+
});
|
|
261
|
+
track(harness);
|
|
262
|
+
expect(pids).toHaveLength(20); // 10 launchers + 10 idlers
|
|
263
|
+
|
|
264
|
+
harness.kill("SIGKILL");
|
|
265
|
+
await assertAllGone(pids, 5_000);
|
|
266
|
+
},
|
|
267
|
+
FUNCTIONAL_TIMEOUT_MS,
|
|
268
|
+
);
|
|
269
|
+
|
|
270
|
+
it(
|
|
271
|
+
"graceful shutdown (stdin close) cleans up every descendant",
|
|
272
|
+
async () => {
|
|
273
|
+
const idler = writeIdler(workDir);
|
|
274
|
+
const { harness, pids } = await spawnHarness({
|
|
275
|
+
workDir,
|
|
276
|
+
count: 3,
|
|
277
|
+
idlerPath: idler,
|
|
278
|
+
});
|
|
279
|
+
track(harness);
|
|
280
|
+
|
|
281
|
+
harness.stdin!.end();
|
|
282
|
+
const exitCode = await new Promise<number | null>((r) =>
|
|
283
|
+
harness.on("exit", (c) => r(c)),
|
|
284
|
+
);
|
|
285
|
+
expect(exitCode).toBe(0);
|
|
286
|
+
await assertAllGone(pids, 5_000);
|
|
287
|
+
},
|
|
288
|
+
FUNCTIONAL_TIMEOUT_MS,
|
|
289
|
+
);
|
|
290
|
+
|
|
291
|
+
it(
|
|
292
|
+
"SIGKILLs stubborn children that ignore SIGTERM",
|
|
293
|
+
async () => {
|
|
294
|
+
const stubborn = writeIdler(workDir, {
|
|
295
|
+
name: "stubborn.mjs",
|
|
296
|
+
ignoreSigterm: true,
|
|
297
|
+
exitOnStdinClose: false,
|
|
298
|
+
});
|
|
299
|
+
const { harness, pids } = await spawnHarness({
|
|
300
|
+
workDir,
|
|
301
|
+
count: 2,
|
|
302
|
+
idlerPath: stubborn,
|
|
303
|
+
});
|
|
304
|
+
track(harness);
|
|
305
|
+
|
|
306
|
+
harness.kill("SIGKILL");
|
|
307
|
+
// Launcher: SIGTERM → 1s grace → SIGKILL. Give 4s headroom.
|
|
308
|
+
await assertAllGone(pids, 4_000);
|
|
309
|
+
},
|
|
310
|
+
FUNCTIONAL_TIMEOUT_MS,
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
it(
|
|
314
|
+
"launcher exits when its supervised child exits on its own",
|
|
315
|
+
async () => {
|
|
316
|
+
const oneShot = writeIdler(workDir, {
|
|
317
|
+
name: "one-shot.mjs",
|
|
318
|
+
selfExitAfterMs: 200,
|
|
319
|
+
});
|
|
320
|
+
const { harness, pids } = await spawnHarness({
|
|
321
|
+
workDir,
|
|
322
|
+
count: 1,
|
|
323
|
+
idlerPath: oneShot,
|
|
324
|
+
});
|
|
325
|
+
track(harness);
|
|
326
|
+
|
|
327
|
+
// Both launcher (pids[0]) and idler (pids[1]) must be gone within seconds
|
|
328
|
+
// even though the harness itself is still running.
|
|
329
|
+
await assertAllGone(pids, 5_000);
|
|
330
|
+
expect(pidAlive(harness.pid!)).toBe(true);
|
|
331
|
+
},
|
|
332
|
+
FUNCTIONAL_TIMEOUT_MS,
|
|
333
|
+
);
|
|
334
|
+
});
|