End all sessions on server startup

This commit is contained in:
Jared Miller 2026-01-30 09:00:11 -05:00
parent 28920eca1e
commit 423fa8375d
Signed by: shmup
GPG key ID: 22B5C6D66A38B06C
3 changed files with 18 additions and 0 deletions

0
data/clarc.db Normal file
View file

View file

@ -31,6 +31,7 @@ let getPendingPromptsStmt: ReturnType<Database["prepare"]>;
let appendOutputStmt: ReturnType<Database["prepare"]>; let appendOutputStmt: ReturnType<Database["prepare"]>;
let getSessionOutputStmt: ReturnType<Database["prepare"]>; let getSessionOutputStmt: ReturnType<Database["prepare"]>;
let updateSessionStatsStmt: ReturnType<Database["prepare"]>; let updateSessionStatsStmt: ReturnType<Database["prepare"]>;
let endAllSessionsStmt: ReturnType<Database["prepare"]>;
// Migration function to add new columns to existing tables // Migration function to add new columns to existing tables
function runMigrations(): void { function runMigrations(): void {
@ -153,6 +154,9 @@ export function initDb(
git_branch = ?, git_files_json = ? git_branch = ?, git_files_json = ?
WHERE id = ? WHERE id = ?
`); `);
endAllSessionsStmt = db.prepare(
"UPDATE sessions SET ended_at = ? WHERE ended_at IS NULL",
);
return db; return db;
} }
@ -198,6 +202,11 @@ export function getActiveSessions(): Session[] {
return getActiveSessionsStmt.all() as Session[]; return getActiveSessionsStmt.all() as Session[];
} }
export function endAllSessions(): number {
const result = endAllSessionsStmt.run(Date.now());
return result.changes;
}
// Prompt functions // Prompt functions
export function createPrompt( export function createPrompt(

View file

@ -7,6 +7,7 @@ import {
createDevice, createDevice,
createPrompt, createPrompt,
createSession, createSession,
endAllSessions,
endSession, endSession,
getActiveSessions, getActiveSessions,
getDeviceBySecret, getDeviceBySecret,
@ -82,6 +83,14 @@ function broadcastSSE(event: SSEEvent): void {
const port = Number.parseInt(process.env.PORT || "7200", 10); const port = Number.parseInt(process.env.PORT || "7200", 10);
initDb(); initDb();
// Clean up ghost sessions from previous runs
const cleanedSessions = endAllSessions();
if (cleanedSessions > 0) {
console.log(
`Cleaned up ${cleanedSessions} ghost session(s) from previous runs`,
);
}
// Auto-seed device from env if configured // Auto-seed device from env if configured
const envSecret = process.env.CLAUDE_REMOTE_SECRET; const envSecret = process.env.CLAUDE_REMOTE_SECRET;
if (envSecret) { if (envSecret) {