feat: harden runtime readiness and session workflow
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
import type { OllamaEnsureResult, ThtRunner } from "../tht/tht-runner.js";
|
||||
|
||||
interface ReadyEntry {
|
||||
expiresAt: number;
|
||||
result: OllamaEnsureResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deduplicates embedding readiness checks and keeps only short-lived successes.
|
||||
* Failures are deliberately not cached so a submit can retry after a transient outage.
|
||||
*/
|
||||
export class ReadinessManager {
|
||||
private inFlight = new Map<string, Promise<OllamaEnsureResult>>();
|
||||
private ready = new Map<string, ReadyEntry>();
|
||||
|
||||
constructor(
|
||||
private tht: ThtRunner,
|
||||
private timeoutSec: number,
|
||||
private ttlMs = 60_000,
|
||||
private now: () => number = Date.now,
|
||||
) {}
|
||||
|
||||
ensure(workspace = ""): Promise<OllamaEnsureResult> {
|
||||
const cached = this.ready.get(workspace);
|
||||
if (cached && cached.expiresAt > this.now()) return Promise.resolve(cached.result);
|
||||
if (cached) this.ready.delete(workspace);
|
||||
|
||||
const current = this.inFlight.get(workspace);
|
||||
if (current) return current;
|
||||
|
||||
const pending = this.tht.ollamaEnsure(workspace, this.timeoutSec)
|
||||
.then((result) => {
|
||||
if (result.ok) {
|
||||
this.ready.set(workspace, { result, expiresAt: this.now() + this.ttlMs });
|
||||
}
|
||||
return result;
|
||||
})
|
||||
.finally(() => {
|
||||
if (this.inFlight.get(workspace) === pending) this.inFlight.delete(workspace);
|
||||
});
|
||||
this.inFlight.set(workspace, pending);
|
||||
return pending;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user