/** * Job Tracker for Async Indexing Operations * * Tracks long-running indexing jobs with progress updates * and status polling support. */ import { type ActiveGraphDiagnostics } from "@massa-th0th/shared"; import type { JobStore } from "./index-job-store.js"; export interface IndexJob { jobId: string; projectId: string; projectPath: string; status: "pending" | "running" | "completed" | "failed"; progress: { current: number; total: number; percentage: number; }; result?: { filesIndexed: number; chunksIndexed: number; errors: number; duration: number; } & Partial>; error?: string; createdAt: Date; startedAt?: Date; completedAt?: Date; /** * Heartbeat timestamp: refreshed on every progress emit and when the job * enters `running`. Used by `reapStaleJobs` to detect a job that has hung * (e.g. an Ollama stall or a mid-flight crash) while the server is still * alive — flipping it to `failed` within the server's lifetime, instead of * waiting for the next process restart to recover it. */ heartbeatAt?: Date; } /** * In-memory job tracker singleton */ export declare class IndexJobTracker { private static instance; private jobs; private readonly MAX_JOBS; private readonly store?; constructor(store?: JobStore); static getInstance(): IndexJobTracker; /** * Create a new indexing job */ createJob(projectId: string, projectPath: string): IndexJob; /** * Get job by ID. Phase 1: lazy-loads from the durable store on a hot miss. */ getJob(jobId: string): IndexJob | undefined; /** * Update job status */ updateStatus(jobId: string, status: IndexJob["status"]): void; /** * Update job progress. Piggybacks the heartbeat: every progress emit refreshes * `heartbeatAt`, which is what `reapStaleJobs` checks. This keeps a healthy * (actively-progressing) job from ever being reaped while catching jobs whose * progress has gone silent. */ updateProgress(jobId: string, current: number, total: number): void; /** * Tick ONLY the heartbeat (no progress change). Use when you want to signal * "still alive" between progress emits (e.g. a long single-file parse that * doesn't increment the file counter). */ heartbeat(jobId: string): void; /** * Reap stale `running` jobs: flip any job whose heartbeat is older than * `staleMs` (or, as a fallback for jobs with no heartbeat, whose `startedAt` * is older than `staleMs`) to `failed`. Covers the case where a job hangs or * crashes mid-flight while the server keeps running — the existing * restart-time `markStaleRunningFailed` only fires on the NEXT process start, * which never happens during a long-lived test suite. * * Returns the count of reaped jobs. */ reapStaleJobs(staleMs: number): number; /** * Set job result on completion */ setResult(jobId: string, result: IndexJob["result"], error?: string): void; setResultAndFlush(jobId: string, result: IndexJob["result"], error?: string): Promise; /** * List all jobs (for debugging/monitoring) */ listJobs(): IndexJob[]; /** * List jobs by project */ listJobsByProject(projectId: string): IndexJob[]; /** * Clean up old jobs (keep last MAX_JOBS). * * Never evict a non-terminal (pending/running) job: a long-running * full-repo index can be dropped before the caller polls, making the job * appear to never reach a terminal state. Only terminal (completed/failed) * jobs are evictable; the oldest terminal job is dropped first. If the cap * can only be met by dropping a non-terminal job (pathological case: more * than MAX_JOBS in-flight jobs at once), the OLDEST non-terminal job is * dropped as an absolute last resort with a warning. */ private cleanupOldJobs; /** * Clear all jobs (for testing) */ clear(): void; } export declare const indexJobTracker: IndexJobTracker; //# sourceMappingURL=index-job-tracker.d.ts.map