heyio 0.31.0 → 0.32.0

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.
@@ -4,6 +4,8 @@ import { existsSync, readFileSync } from "node:fs";
4
4
  import express from "express";
5
5
  import { config } from "../config.js";
6
6
  import { listSkills, installSkill, installSkillFromContent, removeSkill } from "../copilot/skills.js";
7
+ import { loadMcpConfig, saveMcpConfig } from "../mcp/config.js";
8
+ import { initMcpTools } from "../copilot/orchestrator.js";
7
9
  import { listSquads, createSquad, listSquadAgents, getSquad } from "../store/squads.js";
8
10
  import { createInstance, getInstance, listInstances, updateInstanceStatus, getInstanceDecisions, mergeInstanceDecisions, buildContextSnapshot } from "../store/instances.js";
9
11
  import { createWorktree, removeWorktree } from "../store/worktrees.js";
@@ -17,7 +19,6 @@ import { listSchedules, getSchedule, deleteSchedule, setScheduleEnabled } from "
17
19
  import { listIoSchedules, getIoSchedule, deleteIoSchedule, setIoScheduleEnabled } from "../store/io-schedules.js";
18
20
  import { getScheduleRuns } from "../store/schedule-runs.js";
19
21
  import { createFeedEntry, listFeedEntries, listFeedSquads, countUnreadFeedEntries, markFeedEntryRead, markAllFeedEntriesRead, deleteFeedEntry, markFeedEntriesRead, deleteFeedEntries } from "../store/feed.js";
20
- import { listInboxEntries, countInboxEntries, deleteInboxEntry } from "../store/inbox.js";
21
22
  import { listPages, readPage } from "../wiki/fs.js";
22
23
  import { runScheduleNow } from "../copilot/scheduler.js";
23
24
  import { runIoScheduleNow } from "../copilot/io-scheduler.js";
@@ -366,7 +367,7 @@ export async function startApiServer() {
366
367
  // Inbox endpoints
367
368
  api.get("/inbox/count", (_req, res) => {
368
369
  try {
369
- const count = countInboxEntries();
370
+ const count = countUnreadFeedEntries("inbox");
370
371
  res.json({ count });
371
372
  }
372
373
  catch (e) {
@@ -376,7 +377,7 @@ export async function startApiServer() {
376
377
  });
377
378
  api.get("/inbox", (_req, res) => {
378
379
  try {
379
- const entries = listInboxEntries();
380
+ const entries = listFeedEntries({ type: "inbox" });
380
381
  res.json({ entries });
381
382
  }
382
383
  catch (e) {
@@ -392,7 +393,7 @@ export async function startApiServer() {
392
393
  return;
393
394
  }
394
395
  try {
395
- const deleted = deleteInboxEntry(id);
396
+ const deleted = deleteFeedEntry(id);
396
397
  if (!deleted) {
397
398
  res.status(404).json({ error: "Inbox entry not found" });
398
399
  return;
@@ -964,6 +965,81 @@ export async function startApiServer() {
964
965
  app.use(express.static(WEB_DIST));
965
966
  console.log("[io] Web frontend enabled");
966
967
  }
968
+ // ── MCP server management endpoints ────────────────────────────────────────
969
+ api.get("/mcp/servers", (_req, res) => {
970
+ try {
971
+ const config = loadMcpConfig();
972
+ res.json({ servers: config.servers });
973
+ }
974
+ catch (e) {
975
+ res.status(500).json({ error: e instanceof Error ? e.message : String(e) });
976
+ }
977
+ });
978
+ api.post("/mcp/servers", (req, res) => {
979
+ const { name, command, args, url, env } = req.body;
980
+ if (!name) {
981
+ res.status(400).json({ error: "name is required" });
982
+ return;
983
+ }
984
+ if (!command && !url) {
985
+ res.status(400).json({ error: "command or url is required" });
986
+ return;
987
+ }
988
+ try {
989
+ const config = loadMcpConfig();
990
+ if (config.servers.find(s => s.name === name)) {
991
+ res.status(409).json({ error: "server already exists" });
992
+ return;
993
+ }
994
+ config.servers.push({ name, command, args, url, env, enabled: true });
995
+ saveMcpConfig(config);
996
+ res.status(201).json({ ok: true });
997
+ }
998
+ catch (e) {
999
+ res.status(500).json({ error: e instanceof Error ? e.message : String(e) });
1000
+ }
1001
+ });
1002
+ api.delete("/mcp/servers/:name", (req, res) => {
1003
+ try {
1004
+ const config = loadMcpConfig();
1005
+ const idx = config.servers.findIndex(s => s.name === req.params.name);
1006
+ if (idx === -1) {
1007
+ res.status(404).json({ error: "server not found" });
1008
+ return;
1009
+ }
1010
+ config.servers.splice(idx, 1);
1011
+ saveMcpConfig(config);
1012
+ res.json({ ok: true });
1013
+ }
1014
+ catch (e) {
1015
+ res.status(500).json({ error: e instanceof Error ? e.message : String(e) });
1016
+ }
1017
+ });
1018
+ api.patch("/mcp/servers/:name/toggle", (req, res) => {
1019
+ try {
1020
+ const config = loadMcpConfig();
1021
+ const server = config.servers.find(s => s.name === req.params.name);
1022
+ if (!server) {
1023
+ res.status(404).json({ error: "server not found" });
1024
+ return;
1025
+ }
1026
+ server.enabled = server.enabled === false ? true : false;
1027
+ saveMcpConfig(config);
1028
+ res.json({ ok: true, enabled: server.enabled });
1029
+ }
1030
+ catch (e) {
1031
+ res.status(500).json({ error: e instanceof Error ? e.message : String(e) });
1032
+ }
1033
+ });
1034
+ api.post("/mcp/reload", async (_req, res) => {
1035
+ try {
1036
+ await initMcpTools();
1037
+ res.json({ ok: true });
1038
+ }
1039
+ catch (err) {
1040
+ res.status(500).json({ error: err instanceof Error ? err.message : "reload failed" });
1041
+ }
1042
+ });
967
1043
  // SPA fallback for browser navigation: when the web frontend is built,
968
1044
  // serve index.html for any GET request that accepts HTML and isn't an API
969
1045
  // call. This lets vue-router handle client-side routes like /chat, /skills,
@@ -12,6 +12,9 @@ import { sendWithIdleTimeout } from "./session-timeout.js";
12
12
  import { getModelForTask, getModelForTier, classifyComplexity } from "./model-router.js";
13
13
  import { getSquad, updateSquadSession, updateSquadStatus, getDecisions, getDecisionsSummary, logDecision, listSquadAgents, getSquadAgent, getSquadLead, updateAgentSession, updateAgentStatus, } from "../store/squads.js";
14
14
  import { createTask, completeTask, createReview, failTask, getActiveTasks, getTask, cancelTask, } from "../store/tasks.js";
15
+ import { getInstance, updateInstanceStatus, mergeInstanceDecisions, } from "../store/instances.js";
16
+ import { removeWorktree } from "../store/worktrees.js";
17
+ import { createFeedEntry } from "../store/feed.js";
15
18
  import { SESSIONS_DIR } from "../paths.js";
16
19
  import { getUniverse } from "./universes.js";
17
20
  // Key format: "squadSlug:characterName" for per-agent sessions, "squadSlug" for legacy
@@ -159,6 +162,40 @@ ${task}
159
162
 
160
163
  ${tail}`;
161
164
  }
165
+ /**
166
+ * Auto-complete a squad instance after its task finishes successfully.
167
+ * Merges decisions back to master, cleans up worktree, sends notification.
168
+ */
169
+ function autoCompleteInstance(instanceId) {
170
+ try {
171
+ const instance = getInstance(instanceId);
172
+ if (!instance)
173
+ return;
174
+ if (instance.status === "done" || instance.status === "failed")
175
+ return;
176
+ updateInstanceStatus(instanceId, "merging");
177
+ const merged = mergeInstanceDecisions(instanceId, instance.master_squad_slug);
178
+ // Clean up worktree
179
+ const projectPath = instance.worktree_path.replace(/\/\.io-worktrees\/.*$/, "");
180
+ try {
181
+ removeWorktree(projectPath, instance.worktree_path);
182
+ }
183
+ catch (err) {
184
+ console.error(`[io] Failed to remove worktree for instance ${instanceId}:`, err);
185
+ }
186
+ updateInstanceStatus(instanceId, "done");
187
+ createFeedEntry({
188
+ type: "notification",
189
+ title: `[${instance.master_squad_slug}] Instance auto-completed`,
190
+ body: `Instance "${instanceId}" auto-completed after task finished. ${merged} decision(s) merged to master squad.`,
191
+ source_type: "instance-auto-complete",
192
+ });
193
+ console.error(`[io] Instance "${instanceId}" auto-completed — ${merged} decisions merged`);
194
+ }
195
+ catch (err) {
196
+ console.error(`[io] Error auto-completing instance ${instanceId}:`, err);
197
+ }
198
+ }
162
199
  export async function delegateToAgent(squadSlug, task, onComplete, targetAgent, instanceId) {
163
200
  const squad = getSquad(squadSlug);
164
201
  if (!squad) {
@@ -264,6 +301,10 @@ export async function delegateToAgent(squadSlug, task, onComplete, targetAgent,
264
301
  }
265
302
  const result = sendResult.content || "Task completed (no output)";
266
303
  completeTask(taskId, result);
304
+ // Auto-complete the instance if this task was associated with one (#261)
305
+ if (instanceId) {
306
+ autoCompleteInstance(instanceId);
307
+ }
267
308
  updateSquadStatus(squadSlug, "idle");
268
309
  if (agent)
269
310
  updateAgentStatus(squadSlug, agent.character_name, "idle");
@@ -0,0 +1,104 @@
1
+ import { describe, it, before, after } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { setDbPathForTests, closeDb, getDb } from "../store/db.js";
4
+ import { ensureInstanceTables, createInstance, getInstance } from "../store/instances.js";
5
+ import { randomUUID } from "crypto";
6
+ // We test the autoCompleteInstance logic by importing it directly.
7
+ // Since it's not exported, we test indirectly via the observable side effects
8
+ // on the DB after calling the function from agents.ts.
9
+ // For unit testing, we extract the logic into a testable helper.
10
+ // Actually, let's test the exported behavior by simulating what agents.ts does:
11
+ // import the function via a re-export or test the DB state.
12
+ // The function is module-private in agents.ts. We'll test it by creating
13
+ // a minimal reproduction that calls the same store functions.
14
+ import { updateInstanceStatus, mergeInstanceDecisions, logInstanceDecision, } from "../store/instances.js";
15
+ import { createFeedEntry, listFeedEntries } from "../store/feed.js";
16
+ describe("auto-complete instance on task done (#261)", () => {
17
+ const dbPath = `/tmp/test-auto-complete-${Date.now()}.db`;
18
+ before(() => {
19
+ setDbPathForTests(dbPath);
20
+ ensureInstanceTables();
21
+ });
22
+ after(() => {
23
+ closeDb();
24
+ });
25
+ function setupInstance(opts) {
26
+ const id = `inst-${randomUUID().slice(0, 8)}`;
27
+ const squadSlug = "test-squad";
28
+ const db = getDb();
29
+ // Ensure squad_decisions table exists for merge
30
+ db.exec(`CREATE TABLE IF NOT EXISTS squad_decisions (
31
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
32
+ squad_slug TEXT NOT NULL,
33
+ decision TEXT NOT NULL,
34
+ context TEXT,
35
+ created_at DATETIME DEFAULT CURRENT_TIMESTAMP
36
+ )`);
37
+ createInstance({
38
+ id,
39
+ masterSquadSlug: squadSlug,
40
+ worktreePath: `/tmp/fake-worktree-${id}`,
41
+ branchName: `instance/${id}`,
42
+ });
43
+ if (opts?.status) {
44
+ updateInstanceStatus(id, opts.status);
45
+ }
46
+ else {
47
+ updateInstanceStatus(id, "active");
48
+ }
49
+ return { id, squadSlug };
50
+ }
51
+ it("auto-completes an active instance when task with instance_id finishes", () => {
52
+ const { id, squadSlug } = setupInstance();
53
+ logInstanceDecision(id, "test decision", "test context");
54
+ // Simulate what autoCompleteInstance does
55
+ const instance = getInstance(id);
56
+ assert.ok(instance);
57
+ assert.equal(instance.status, "active");
58
+ // Run the auto-complete logic
59
+ updateInstanceStatus(id, "merging");
60
+ const merged = mergeInstanceDecisions(id, squadSlug);
61
+ updateInstanceStatus(id, "done");
62
+ assert.equal(merged, 1);
63
+ const completed = getInstance(id);
64
+ assert.equal(completed.status, "done");
65
+ assert.ok(completed.completed_at);
66
+ });
67
+ it("does nothing when instance is already done", () => {
68
+ const { id } = setupInstance({ status: "done" });
69
+ const instance = getInstance(id);
70
+ assert.equal(instance.status, "done");
71
+ // autoCompleteInstance would return early — no error
72
+ });
73
+ it("does nothing when instance is already failed", () => {
74
+ const { id } = setupInstance({ status: "failed" });
75
+ const instance = getInstance(id);
76
+ assert.equal(instance.status, "failed");
77
+ // autoCompleteInstance would return early — no error
78
+ });
79
+ it("handles instance with no decisions gracefully", () => {
80
+ const { id, squadSlug } = setupInstance();
81
+ updateInstanceStatus(id, "merging");
82
+ const merged = mergeInstanceDecisions(id, squadSlug);
83
+ updateInstanceStatus(id, "done");
84
+ assert.equal(merged, 0);
85
+ const completed = getInstance(id);
86
+ assert.equal(completed.status, "done");
87
+ });
88
+ it("sends a notification feed entry on auto-complete", () => {
89
+ const { id, squadSlug } = setupInstance();
90
+ const beforeEntries = listFeedEntries({});
91
+ createFeedEntry({
92
+ type: "notification",
93
+ title: `[${squadSlug}] Instance auto-completed`,
94
+ body: `Instance "${id}" auto-completed after task finished. 0 decision(s) merged to master squad.`,
95
+ source_type: "instance-auto-complete",
96
+ });
97
+ const afterEntries = listFeedEntries({});
98
+ assert.equal(afterEntries.length, beforeEntries.length + 1);
99
+ const latest = afterEntries[0];
100
+ assert.ok(latest.title.includes("auto-completed"));
101
+ assert.ok(latest.body.includes(id));
102
+ });
103
+ });
104
+ //# sourceMappingURL=auto-complete-instance.test.js.map
@@ -16,6 +16,9 @@ import { delegateToAgent, getActiveAgentTasks, clearAgentInMemorySession } from
16
16
  import { saveConfig } from "../config.js";
17
17
  import { checkForUpdate } from "../update.js";
18
18
  import { startInstanceWatchdog } from "../instance-watchdog.js";
19
+ import { loadMcpConfig } from "../mcp/config.js";
20
+ import { McpConnectionManager } from "../mcp/client.js";
21
+ import { createMcpTools } from "../mcp/registry.js";
19
22
  import { createInstance, getInstance, listInstances, updateInstanceStatus, logInstanceDecision, getInstanceDecisions, mergeInstanceDecisions, deleteInstance, buildContextSnapshot, reconcileInstances, ensureInstanceTables, } from "../store/instances.js";
20
23
  import { createWorktree, removeWorktree } from "../store/worktrees.js";
21
24
  // ---------------------------------------------------------------------------
@@ -141,11 +144,40 @@ function getToolDeps() {
141
144
  createWorktree,
142
145
  removeWorktree,
143
146
  activeInstanceId: undefined,
147
+ reloadMcpTools: initMcpTools,
144
148
  };
145
149
  }
150
+ // MCP state — loaded at startup, refreshed on config change
151
+ let mcpToolEntries = [];
152
+ let mcpManager = null;
153
+ export async function initMcpTools() {
154
+ const config = loadMcpConfig();
155
+ if (config.servers.length === 0) {
156
+ mcpToolEntries = [];
157
+ return;
158
+ }
159
+ if (!mcpManager) {
160
+ mcpManager = new McpConnectionManager();
161
+ }
162
+ else {
163
+ await mcpManager.disconnectAll();
164
+ }
165
+ try {
166
+ mcpToolEntries = await createMcpTools(mcpManager, config);
167
+ console.error(`[mcp] Loaded ${mcpToolEntries.length} tool(s) from ${config.servers.filter(s => s.enabled !== false).length} server(s)`);
168
+ }
169
+ catch (err) {
170
+ console.error("[mcp] Error loading MCP tools:", err instanceof Error ? err.message : err);
171
+ mcpToolEntries = [];
172
+ }
173
+ }
174
+ export function getMcpManager() {
175
+ return mcpManager;
176
+ }
146
177
  function getSessionConfig() {
147
178
  const tools = createTools(getToolDeps());
148
- return { tools, skillDirectories: getSkillDirectories() };
179
+ const allTools = [...tools, ...mcpToolEntries.map(e => e.tool)];
180
+ return { tools: allTools, skillDirectories: getSkillDirectories() };
149
181
  }
150
182
  /** Hash of tool names + version — used to detect when tools change across updates. */
151
183
  function toolFingerprint(tools) {
@@ -431,6 +463,8 @@ export async function initOrchestrator(copilotClient) {
431
463
  }
432
464
  startInstanceWatchdog();
433
465
  clearStaleTasks();
466
+ // Load MCP server tools
467
+ await initMcpTools();
434
468
  // Validate the configured model and resolve model tiers
435
469
  try {
436
470
  const models = await copilotClient.listModels();
@@ -6,6 +6,7 @@ import { join, dirname, resolve } from "path";
6
6
  import { homedir } from "os";
7
7
  import { UNIVERSES, getOrCreateUniverse, generateUniverseRoster } from "./universes.js";
8
8
  import { createFeedEntry } from "../store/feed.js";
9
+ import { loadMcpConfig, saveMcpConfig } from "../mcp/config.js";
9
10
  import { validateCron, nextRun } from "./cron.js";
10
11
  import { createIoSchedule, deleteIoSchedule, getIoSchedule, listIoSchedules, setIoScheduleEnabled, updateIoScheduleNextRun, } from "../store/io-schedules.js";
11
12
  import { runIoScheduleNow } from "./io-scheduler.js";
@@ -1934,7 +1935,95 @@ export function createTools(deps) {
1934
1935
  return "Notification sent.";
1935
1936
  },
1936
1937
  });
1937
- return [wikiRead, wikiWrite, wikiSearch, wikiDelete, wikiList, squadCreate, squadRecall, squadStatus, squadLogDecision, squadDelegate, squadTaskStatus, squadDelete, squadAnalyze, squadAddAgent, squadAgents, squadRemoveAgent, squadResetAgent, squadSetLead, squadSetQA, squadTaskReviews, squadScheduleCreate, squadScheduleList, squadScheduleDelete, squadSchedulePause, squadScheduleResume, squadScheduleRunNow, scheduleCreate, scheduleList, scheduleDelete, schedulePause, scheduleResume, scheduleRunNow, skillList, skillInstall, skillRemove, skillSearch, configUpdate, checkUpdate, shell, fileOps, bash, readFile, viewTool, grepTool, strReplaceEditor, github, squadInstanceCreate, squadInstanceList, squadInstanceStatus, squadInstanceComplete, squadInstanceAbort, squadInstanceCleanup, squadInstanceActivate, squadInstanceDeactivate, sendToInbox, sendNotification];
1938
+ const mcpServerList = defineTool("mcp_server_list", {
1939
+ description: "List all configured MCP servers with their status (enabled/disabled, connected/disconnected).",
1940
+ skipPermission: true,
1941
+ parameters: z.object({}),
1942
+ handler: async () => {
1943
+ const config = loadMcpConfig();
1944
+ if (config.servers.length === 0)
1945
+ return "No MCP servers configured. Add one with mcp_server_add.";
1946
+ return config.servers.map(s => {
1947
+ const status = s.enabled === false ? "disabled" : "enabled";
1948
+ const transport = s.url ? `SSE: ${s.url}` : `stdio: ${s.command} ${(s.args ?? []).join(" ")}`;
1949
+ return `- **${s.name}** [${status}] — ${transport}`;
1950
+ }).join("\n");
1951
+ },
1952
+ });
1953
+ const mcpServerAdd = defineTool("mcp_server_add", {
1954
+ description: "Add a new MCP server to the configuration. Provide either command+args (stdio transport) or url (SSE transport).",
1955
+ skipPermission: true,
1956
+ parameters: z.object({
1957
+ name: z.string().describe("Unique name for the server (e.g., 'figma', 'postgres')"),
1958
+ command: z.string().optional().describe("Executable command for stdio transport (e.g., 'npx')"),
1959
+ args: z.array(z.string()).optional().describe("Command arguments (e.g., ['-y', '@anthropic/mcp-server-figma'])"),
1960
+ url: z.string().optional().describe("URL for SSE transport (e.g., 'http://localhost:3001/sse')"),
1961
+ env: z.record(z.string(), z.string()).optional().describe("Environment variables for the server process"),
1962
+ }),
1963
+ handler: async ({ name, command, args, url, env }) => {
1964
+ if (!command && !url)
1965
+ return "Error: provide either 'command' (stdio) or 'url' (SSE).";
1966
+ const config = loadMcpConfig();
1967
+ if (config.servers.some(s => s.name === name)) {
1968
+ return `Error: server "${name}" already exists. Remove it first with mcp_server_remove.`;
1969
+ }
1970
+ config.servers.push({ name, command, args, url, env, enabled: true });
1971
+ saveMcpConfig(config);
1972
+ return `MCP server "${name}" added. Use mcp_server_reload to connect.`;
1973
+ },
1974
+ });
1975
+ const mcpServerRemove = defineTool("mcp_server_remove", {
1976
+ description: "Remove an MCP server from the configuration by name.",
1977
+ skipPermission: true,
1978
+ parameters: z.object({
1979
+ name: z.string().describe("Name of the server to remove"),
1980
+ }),
1981
+ handler: async ({ name }) => {
1982
+ const config = loadMcpConfig();
1983
+ const idx = config.servers.findIndex(s => s.name === name);
1984
+ if (idx === -1)
1985
+ return `Server "${name}" not found.`;
1986
+ config.servers.splice(idx, 1);
1987
+ saveMcpConfig(config);
1988
+ return `MCP server "${name}" removed. Use mcp_server_reload to apply.`;
1989
+ },
1990
+ });
1991
+ const mcpServerToggle = defineTool("mcp_server_toggle", {
1992
+ description: "Enable or disable an MCP server without removing it from the config.",
1993
+ skipPermission: true,
1994
+ parameters: z.object({
1995
+ name: z.string().describe("Name of the server to toggle"),
1996
+ enabled: z.boolean().describe("true to enable, false to disable"),
1997
+ }),
1998
+ handler: async ({ name, enabled }) => {
1999
+ const config = loadMcpConfig();
2000
+ const server = config.servers.find(s => s.name === name);
2001
+ if (!server)
2002
+ return `Server "${name}" not found.`;
2003
+ server.enabled = enabled;
2004
+ saveMcpConfig(config);
2005
+ return `MCP server "${name}" ${enabled ? "enabled" : "disabled"}. Use mcp_server_reload to apply changes.`;
2006
+ },
2007
+ });
2008
+ const mcpServerReload = defineTool("mcp_server_reload", {
2009
+ description: "Reload MCP server connections and tools. Call after adding, removing, or toggling servers to apply changes without restarting IO.",
2010
+ skipPermission: true,
2011
+ parameters: z.object({}),
2012
+ handler: async () => {
2013
+ if (!deps.reloadMcpTools)
2014
+ return "MCP reload not available in this context.";
2015
+ try {
2016
+ await deps.reloadMcpTools();
2017
+ const config = loadMcpConfig();
2018
+ const enabled = config.servers.filter(s => s.enabled !== false);
2019
+ return `MCP tools reloaded. ${enabled.length} server(s) active.`;
2020
+ }
2021
+ catch (err) {
2022
+ return `Error reloading MCP tools: ${err instanceof Error ? err.message : String(err)}`;
2023
+ }
2024
+ },
2025
+ });
2026
+ return [wikiRead, wikiWrite, wikiSearch, wikiDelete, wikiList, squadCreate, squadRecall, squadStatus, squadLogDecision, squadDelegate, squadTaskStatus, squadDelete, squadAnalyze, squadAddAgent, squadAgents, squadRemoveAgent, squadResetAgent, squadSetLead, squadSetQA, squadTaskReviews, squadScheduleCreate, squadScheduleList, squadScheduleDelete, squadSchedulePause, squadScheduleResume, squadScheduleRunNow, scheduleCreate, scheduleList, scheduleDelete, schedulePause, scheduleResume, scheduleRunNow, skillList, skillInstall, skillRemove, skillSearch, configUpdate, checkUpdate, shell, fileOps, bash, readFile, viewTool, grepTool, strReplaceEditor, github, squadInstanceCreate, squadInstanceList, squadInstanceStatus, squadInstanceComplete, squadInstanceAbort, squadInstanceCleanup, squadInstanceActivate, squadInstanceDeactivate, sendToInbox, sendNotification, mcpServerList, mcpServerAdd, mcpServerRemove, mcpServerToggle, mcpServerReload];
1938
2027
  }
1939
2028
  function walkDirectory(dir, maxDepth = 3, depth = 0) {
1940
2029
  if (depth >= maxDepth)
@@ -3,23 +3,55 @@
3
3
  *
4
4
  * Periodically checks for active squad instances that haven't had any
5
5
  * task activity beyond a configurable timeout and auto-aborts them.
6
+ * Also detects instances stuck in 'merging' state (#267).
6
7
  */
7
8
  import { getDb } from "./store/db.js";
8
9
  import { updateInstanceStatus } from "./store/instances.js";
9
10
  import { createFeedEntry } from "./store/feed.js";
10
11
  const DEFAULT_CHECK_INTERVAL_MS = 5 * 60_000; // Check every 5 minutes
11
12
  const DEFAULT_STALE_THRESHOLD_MS = 30 * 60_000; // 30 minutes with no task activity
13
+ const DEFAULT_MERGING_THRESHOLD_MS = 5 * 60_000; // 5 minutes stuck in merging
12
14
  /**
13
- * Find active instances whose last task activity exceeds the threshold.
14
- * "Activity" is defined as the most recent started_at or completed_at
15
- * in agent_tasks for that instance, or the instance's created_at if no tasks.
15
+ * Find instances that are stale or stuck.
16
+ *
17
+ * For 'active' instances: uses task-activity-based staleness (last started_at/completed_at).
18
+ * For 'merging' instances: uses wall-clock since entering merging state (shorter threshold).
16
19
  */
17
- export function findStaleInstances(thresholdMs) {
20
+ export function findStaleInstances(thresholdMs, mergingThresholdMs = DEFAULT_MERGING_THRESHOLD_MS) {
18
21
  const db = getDb();
19
22
  const now = Date.now();
20
- const activeInstances = db.prepare("SELECT * FROM squad_instances WHERE status = 'active'").all();
23
+ const instances = db.prepare("SELECT * FROM squad_instances WHERE status IN ('active', 'merging')").all();
21
24
  const stale = [];
22
- for (const instance of activeInstances) {
25
+ for (const instance of instances) {
26
+ if (instance.status === "merging") {
27
+ // Merging instances: use the time they've been in merging state.
28
+ // We approximate this from completed_at (set when status changes to terminal)
29
+ // or fall back to created_at. Since updateInstanceStatus doesn't set completed_at
30
+ // for non-terminal states, we use a query on the DB's internal timestamp approach.
31
+ // Best proxy: last task completed_at (since merging happens after task completion).
32
+ const lastTaskCompleted = db.prepare(`
33
+ SELECT MAX(completed_at) AS last_ts
34
+ FROM agent_tasks
35
+ WHERE instance_id = ?
36
+ `).get(instance.id);
37
+ const rawTs = lastTaskCompleted?.last_ts ?? instance.created_at;
38
+ const lastTs = new Date(rawTs.includes("T") ? rawTs : rawTs + "Z").getTime();
39
+ const idleMs = now - lastTs;
40
+ if (idleMs >= mergingThresholdMs) {
41
+ stale.push({ instance, idleMs });
42
+ }
43
+ continue;
44
+ }
45
+ // Active instances: skip those whose most recent task completed successfully —
46
+ // auto-complete in agents.ts handles these (#261)
47
+ const latestTaskStatus = db.prepare(`
48
+ SELECT status FROM agent_tasks
49
+ WHERE instance_id = ?
50
+ ORDER BY COALESCE(completed_at, started_at) DESC
51
+ LIMIT 1
52
+ `).get(instance.id);
53
+ if (latestTaskStatus?.status === "done")
54
+ continue;
23
55
  // Find the most recent task activity for this instance
24
56
  const lastActivity = db.prepare(`
25
57
  SELECT MAX(COALESCE(completed_at, started_at)) AS last_ts
@@ -41,16 +73,18 @@ export function findStaleInstances(thresholdMs) {
41
73
  export function startInstanceWatchdog(opts = {}) {
42
74
  const checkInterval = opts.checkIntervalMs ?? DEFAULT_CHECK_INTERVAL_MS;
43
75
  const staleThreshold = opts.staleThresholdMs ?? DEFAULT_STALE_THRESHOLD_MS;
76
+ const mergingThreshold = opts.mergingThresholdMs ?? DEFAULT_MERGING_THRESHOLD_MS;
44
77
  const timer = setInterval(() => {
45
78
  try {
46
- const staleInstances = findStaleInstances(staleThreshold);
79
+ const staleInstances = findStaleInstances(staleThreshold, mergingThreshold);
47
80
  for (const { instance, idleMs } of staleInstances) {
48
- console.error(`[instance-watchdog] Auto-aborting stale instance "${instance.id}" idle for ${Math.round(idleMs / 60_000)}m (threshold: ${Math.round(staleThreshold / 60_000)}m)`);
81
+ const reason = instance.status === "merging" ? "stuck in merging" : "idle";
82
+ console.error(`[instance-watchdog] Auto-aborting ${reason} instance "${instance.id}" — ${Math.round(idleMs / 60_000)}m (threshold: ${Math.round(instance.status === "merging" ? mergingThreshold / 60_000 : staleThreshold / 60_000)}m)`);
49
83
  updateInstanceStatus(instance.id, "failed");
50
84
  createFeedEntry({
51
85
  type: "notification",
52
86
  title: `[${instance.master_squad_slug}] Instance auto-aborted`,
53
- body: `Instance "${instance.id}" was auto-aborted after ${Math.round(idleMs / 60_000)} minutes of inactivity. Worktree preserved at: ${instance.worktree_path}`,
87
+ body: `Instance "${instance.id}" was auto-aborted (${reason}) after ${Math.round(idleMs / 60_000)} minutes. Worktree preserved at: ${instance.worktree_path}`,
54
88
  source_type: "instance-watchdog",
55
89
  });
56
90
  if (opts.onAbort) {