Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/agents/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,34 @@ export class AgentManager {
}

async reconcileAgents(): Promise<void> {
await this.reconcileAgentStatuses();
}

async reconcileAgentStatuses(): Promise<AgentRecord[]> {
const result = await this.pool.query(
"SELECT id, tmux_session AS \"tmuxSession\" FROM agents WHERE status IN ('running', 'creating', 'unknown')"
"SELECT id, tmux_session AS \"tmuxSession\", status FROM agents WHERE status IN ('running', 'stopping', 'creating')"
);

for (const row of result.rows as Array<{ id: string; tmuxSession: string | null }>) {
const reconciled: AgentRecord[] = [];

for (const row of result.rows as Array<{ id: string; tmuxSession: string | null; status: string }>) {
const exists = row.tmuxSession ? await this.tmuxHasSession(row.tmuxSession) : false;
await this.setAgentStatus(row.id, exists ? "running" : "stopped", null, row.tmuxSession ?? undefined);

if (!exists) {
await this.setAgentStatus(row.id, "stopped", null, row.tmuxSession ?? undefined);
await this.setSystemLatestEvent(row.id, {
type: "idle",
message: "Session ended unexpectedly.",
metadata: { source: "system" }
});
const agent = await this.getAgent(row.id);
if (agent) {
reconciled.push(agent);
}
}
}

return reconciled;
}

private async startTmuxSession(
Expand Down
34 changes: 34 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ const gitRefreshCounters = {
};
let gitContextRefreshTimer: NodeJS.Timeout | null = null;

const AGENT_STATUS_RECONCILE_INTERVAL_MS = 30_000;
let agentStatusReconcileTimer: NodeJS.Timeout | null = null;

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const webDistDir = path.resolve(__dirname, "../web/dist");
Expand Down Expand Up @@ -1234,6 +1237,7 @@ async function start() {
const agents = await agentManager.listAgents();
queueGitContextRefresh(agents.map((agent) => agent.id));
startGitContextRefreshLoop();
startAgentStatusReconcileLoop();
await registerRoutes();

const protocol = config.tls ? "https" : "http";
Expand Down Expand Up @@ -1396,6 +1400,35 @@ function stopGitContextRefreshLoop(): void {
gitContextRefreshTimer = null;
}

function startAgentStatusReconcileLoop(): void {
if (agentStatusReconcileTimer) {
return;
}
agentStatusReconcileTimer = setInterval(() => {
void runAgentStatusReconciliation();
}, AGENT_STATUS_RECONCILE_INTERVAL_MS);
}

function stopAgentStatusReconcileLoop(): void {
if (!agentStatusReconcileTimer) {
return;
}
clearInterval(agentStatusReconcileTimer);
agentStatusReconcileTimer = null;
}

async function runAgentStatusReconciliation(): Promise<void> {
try {
const reconciled = await agentManager.reconcileAgentStatuses();
for (const agent of reconciled) {
console.log(`[reconcile] Agent ${agent.id} (${agent.name}) status corrected to stopped`);
uiEventBroker.publish({ type: "agent.upsert", agent: withStreamFlag(agent) });
}
} catch (error) {
app.log.warn({ err: error }, "Agent status reconciliation failed.");
}
}

async function refreshAllAgentGitContexts(): Promise<void> {
try {
const agents = await agentManager.listAgents();
Expand Down Expand Up @@ -1791,6 +1824,7 @@ async function shutdown(code: number): Promise<void> {

streamManager.stopAll();
stopGitContextRefreshLoop();
stopAgentStatusReconcileLoop();
await pool.end().catch(() => null);
await app.close().catch(() => null);
process.exit(code);
Expand Down
Loading