Refactor setup and tool helpers for improved modularity and clarity

- Moved setup logic into dedicated files: agent.ts, config.ts, index.ts, workspace.ts.
- Introduced tool-helpers.ts for shared functions across tools, reducing boilerplate.
- Updated tools (status, task-comment, task-create, task-update, work-finish, work-start) to utilize new helper functions for workspace resolution and provider creation.
- Enhanced error handling and context detection in tools.
- Improved project resolution logic to streamline tool execution.
- Added new functionality for agent creation and configuration management in setup.
This commit is contained in:
Lauren ten Hoor
2026-02-10 22:51:35 +08:00
parent 55b062ac76
commit 70af40e986
22 changed files with 768 additions and 953 deletions

View File

@@ -6,7 +6,7 @@
*/
import type { StateLabel } from "../providers/provider.js";
import {
getSessionForModel,
getSessionForTier,
getWorker,
updateWorker,
type Project,
@@ -19,7 +19,7 @@ export type HealthIssue = {
groupId: string;
role: "dev" | "qa";
message: string;
model?: string | null;
tier?: string | null;
sessionKey?: string | null;
hoursActive?: number;
issueId?: string | null;
@@ -46,7 +46,7 @@ export async function checkWorkerHealth(opts: {
const { workspaceDir, groupId, project, role, activeSessions, autoFix, provider } = opts;
const fixes: HealthFix[] = [];
const worker = getWorker(project, role);
const sessionKey = worker.model ? getSessionForModel(worker, worker.model) : null;
const sessionKey = worker.tier ? getSessionForTier(worker, worker.tier) : null;
const revertLabel: StateLabel = role === "dev" ? "To Do" : "To Test";
const currentLabel: StateLabel = role === "dev" ? "Doing" : "Testing";
@@ -62,14 +62,14 @@ export async function checkWorkerHealth(opts: {
}
}
// Check 1: Active but no session key for current model
// Check 1: Active but no session key for current tier
if (worker.active && !sessionKey) {
const fix: HealthFix = {
issue: {
type: "active_no_session", severity: "critical",
project: project.name, groupId, role,
model: worker.model,
message: `${role.toUpperCase()} active but no session for model "${worker.model}"`,
tier: worker.tier,
message: `${role.toUpperCase()} active but no session for tier "${worker.tier}"`,
},
fixed: false,
};
@@ -86,7 +86,7 @@ export async function checkWorkerHealth(opts: {
issue: {
type: "zombie_session", severity: "critical",
project: project.name, groupId, role,
sessionKey, model: worker.model,
sessionKey, tier: worker.tier,
message: `${role.toUpperCase()} session not in active sessions list`,
},
fixed: false,
@@ -94,7 +94,7 @@ export async function checkWorkerHealth(opts: {
if (autoFix) {
await revertIssueLabel(fix);
const sessions = { ...worker.sessions };
if (worker.model) sessions[worker.model] = null;
if (worker.tier) sessions[worker.tier] = null;
await updateWorker(workspaceDir, groupId, role, { active: false, issueId: null, sessions });
fix.fixed = true;
}

View File

@@ -7,10 +7,10 @@
import type { Issue, StateLabel } from "../providers/provider.js";
import type { IssueProvider } from "../providers/provider.js";
import { createProvider } from "../providers/index.js";
import { selectModel } from "../model-selector.js";
import { getWorker, getSessionForModel, readProjects } from "../projects.js";
import { selectTier } from "../model-selector.js";
import { getWorker, getSessionForTier, readProjects } from "../projects.js";
import { dispatchTask } from "../dispatch.js";
import type { Tier } from "../tiers.js";
import { ALL_TIERS, type Tier } from "../tiers.js";
// ---------------------------------------------------------------------------
// Shared constants + helpers (used by tick, work-start, auto-pickup)
@@ -20,11 +20,9 @@ export const DEV_LABELS: StateLabel[] = ["To Do", "To Improve"];
export const QA_LABELS: StateLabel[] = ["To Test"];
export const PRIORITY_ORDER: StateLabel[] = ["To Improve", "To Test", "To Do"];
const TIER_LABELS: Tier[] = ["junior", "medior", "senior", "qa"];
export function detectTierFromLabels(labels: string[]): Tier | null {
const lower = labels.map((l) => l.toLowerCase());
return TIER_LABELS.find((t) => lower.includes(t)) ?? null;
return (ALL_TIERS as readonly string[]).find((t) => lower.includes(t)) as Tier | undefined ?? null;
}
export function detectRoleFromLabel(label: StateLabel): "dev" | "qa" | null {
@@ -78,7 +76,7 @@ export type TickAction = {
issueId: number;
issueTitle: string;
role: "dev" | "qa";
model: string;
tier: string;
sessionAction: "spawn" | "send";
announcement: string;
};
@@ -145,21 +143,13 @@ export async function projectTick(opts: {
const targetLabel: StateLabel = role === "dev" ? "Doing" : "Testing";
// Tier selection: label → heuristic
let modelAlias: string;
const tier = detectTierFromLabels(issue.labels);
if (tier) {
if (role === "qa" && tier !== "qa") modelAlias = "qa";
else if (role === "dev" && tier === "qa") modelAlias = selectModel(issue.title, issue.description ?? "", role).tier;
else modelAlias = tier;
} else {
modelAlias = selectModel(issue.title, issue.description ?? "", role).tier;
}
const selectedTier = resolveTierForIssue(issue, role);
if (dryRun) {
pickups.push({
project: project.name, groupId, issueId: issue.iid, issueTitle: issue.title,
role, model: modelAlias,
sessionAction: getSessionForModel(worker, modelAlias) ? "send" : "spawn",
role, tier: selectedTier,
sessionAction: getSessionForTier(worker, selectedTier) ? "send" : "spawn",
announcement: `[DRY RUN] Would pick up #${issue.iid}`,
});
} else {
@@ -167,13 +157,13 @@ export async function projectTick(opts: {
const dr = await dispatchTask({
workspaceDir, agentId, groupId, project: fresh, issueId: issue.iid,
issueTitle: issue.title, issueDescription: issue.description ?? "", issueUrl: issue.web_url,
role, modelAlias, fromLabel: currentLabel, toLabel: targetLabel,
role, tier: selectedTier, fromLabel: currentLabel, toLabel: targetLabel,
transitionLabel: (id, from, to) => provider.transitionLabel(id, from as StateLabel, to as StateLabel),
pluginConfig, sessionKey,
});
pickups.push({
project: project.name, groupId, issueId: issue.iid, issueTitle: issue.title,
role, model: dr.modelAlias, sessionAction: dr.sessionAction, announcement: dr.announcement,
role, tier: dr.tier, sessionAction: dr.sessionAction, announcement: dr.announcement,
});
} catch (err) {
skipped.push({ role, reason: `Dispatch failed: ${(err as Error).message}` });
@@ -185,3 +175,20 @@ export async function projectTick(opts: {
return { pickups, skipped };
}
// ---------------------------------------------------------------------------
// Private helpers
// ---------------------------------------------------------------------------
/**
* Determine the tier for an issue based on labels, role overrides, and heuristic fallback.
*/
function resolveTierForIssue(issue: Issue, role: "dev" | "qa"): string {
const labelTier = detectTierFromLabels(issue.labels);
if (labelTier) {
if (role === "qa" && labelTier !== "qa") return "qa";
if (role === "dev" && labelTier === "qa") return selectTier(issue.title, issue.description ?? "", role).tier;
return labelTier;
}
return selectTier(issue.title, issue.description ?? "", role).tier;
}