refactor(subagents): share run target resolution

This commit is contained in:
Peter Steinberger
2026-02-18 18:09:20 +00:00
parent 4e7182c4af
commit bec94449eb
4 changed files with 260 additions and 138 deletions

View File

@@ -37,12 +37,13 @@ import { INTERNAL_MESSAGE_CHANNEL } from "../../utils/message-channel.js";
import { stopSubagentsForRequester } from "./abort.js";
import type { CommandHandler } from "./commands-types.js";
import { clearSessionQueues } from "./queue.js";
import { formatRunLabel, formatRunStatus, sortSubagentRuns } from "./subagents-utils.js";
type SubagentTargetResolution = {
entry?: SubagentRunRecord;
error?: string;
};
import {
formatRunLabel,
formatRunStatus,
resolveSubagentTargetFromRuns,
type SubagentTargetResolution,
sortSubagentRuns,
} from "./subagents-utils.js";
const COMMAND = "/subagents";
const COMMAND_KILL = "/kill";
@@ -138,56 +139,21 @@ function resolveSubagentTarget(
runs: SubagentRunRecord[],
token: string | undefined,
): SubagentTargetResolution {
const trimmed = token?.trim();
if (!trimmed) {
return { error: "Missing subagent id." };
}
if (trimmed === "last") {
const sorted = sortSubagentRuns(runs);
return { entry: sorted[0] };
}
const sorted = sortSubagentRuns(runs);
const recentCutoff = Date.now() - RECENT_WINDOW_MINUTES * 60_000;
const numericOrder = [
...sorted.filter((entry) => !entry.endedAt),
...sorted.filter((entry) => !!entry.endedAt && (entry.endedAt ?? 0) >= recentCutoff),
];
if (/^\d+$/.test(trimmed)) {
const idx = Number.parseInt(trimmed, 10);
if (!Number.isFinite(idx) || idx <= 0 || idx > numericOrder.length) {
return { error: `Invalid subagent index: ${trimmed}` };
}
return { entry: numericOrder[idx - 1] };
}
if (trimmed.includes(":")) {
const match = runs.find((entry) => entry.childSessionKey === trimmed);
return match ? { entry: match } : { error: `Unknown subagent session: ${trimmed}` };
}
const lowered = trimmed.toLowerCase();
const byLabel = runs.filter((entry) => formatRunLabel(entry).toLowerCase() === lowered);
if (byLabel.length === 1) {
return { entry: byLabel[0] };
}
if (byLabel.length > 1) {
return { error: `Ambiguous subagent label: ${trimmed}` };
}
const byLabelPrefix = runs.filter((entry) =>
formatRunLabel(entry).toLowerCase().startsWith(lowered),
);
if (byLabelPrefix.length === 1) {
return { entry: byLabelPrefix[0] };
}
if (byLabelPrefix.length > 1) {
return { error: `Ambiguous subagent label prefix: ${trimmed}` };
}
const byRunId = runs.filter((entry) => entry.runId.startsWith(trimmed));
if (byRunId.length === 1) {
return { entry: byRunId[0] };
}
if (byRunId.length > 1) {
return { error: `Ambiguous run id prefix: ${trimmed}` };
}
return { error: `Unknown subagent id: ${trimmed}` };
return resolveSubagentTargetFromRuns({
runs,
token,
recentWindowMinutes: RECENT_WINDOW_MINUTES,
label: (entry) => formatRunLabel(entry),
errors: {
missingTarget: "Missing subagent id.",
invalidIndex: (value) => `Invalid subagent index: ${value}`,
unknownSession: (value) => `Unknown subagent session: ${value}`,
ambiguousLabel: (value) => `Ambiguous subagent label: ${value}`,
ambiguousLabelPrefix: (value) => `Ambiguous subagent label prefix: ${value}`,
ambiguousRunIdPrefix: (value) => `Ambiguous run id prefix: ${value}`,
unknownTarget: (value) => `Unknown subagent id: ${value}`,
},
});
}
function buildSubagentsHelp() {