← back to PrimeLocus__bmo

Function bodies 413 total

All specs Real LLM only Function bodies
getThoughtSystem function · typescript · L20-L30 (11 LOC)
beau-terminal/src/lib/server/thoughts/index.ts
export function getThoughtSystem() {
  if (!_queue) return null;
  return {
    queue: _queue,
    publishSurfaced(thought: { id: string; type: string; text: string | null; trigger: string; novelty: number }) {
      if (_publish && _topics) {
        _publish(_topics.thoughts.surfaced, JSON.stringify(thought));
      }
    },
  };
}
PressureEngine class · typescript · L65-L278 (214 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
export class PressureEngine {
	/** @internal exposed for test access via (engine as any)._state */
	_state: PressureState;
	private _config: PressureConfig;
	private _lastTrigger: string;
	private _wasNoveltySpike: boolean;

	constructor(configOverrides: Partial<PressureConfig> = {}) {
		this._config = { ...DEFAULT_CONFIG, ...configOverrides };
		this._state = {
			value: 0,
			lastSurfacedAt: null,
			cooldownUntil: null,
			baselines: {},
			baselineInitialized: {},
		};
		this._lastTrigger = 'none';
		this._wasNoveltySpike = false;
	}

	// ── Core tick ─────────────────────────────────────────────────────────────

	/**
	 * Advance the pressure engine by one tick (~5 seconds).
	 *
	 * @param beauState  - Live BeauState (vector + sleepState)
	 * @param sleepState - Current sleep state string (redundant but explicit)
	 * @param dailyBudget - Current daily budget status (unused in tick; kept for API symmetry)
	 * @param rng        - Optional seeded RNG (defaults to Math.random)
	 */
	ti
constructor method · typescript · L72-L83 (12 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	constructor(configOverrides: Partial<PressureConfig> = {}) {
		this._config = { ...DEFAULT_CONFIG, ...configOverrides };
		this._state = {
			value: 0,
			lastSurfacedAt: null,
			cooldownUntil: null,
			baselines: {},
			baselineInitialized: {},
		};
		this._lastTrigger = 'none';
		this._wasNoveltySpike = false;
	}
tick method · typescript · L95-L167 (73 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	tick(
		beauState: PressureBeauState,
		sleepState: string,
		_dailyBudget: DailyBudgetStatus,
		rng: () => number = Math.random,
	): void {
		const now = Date.now();
		const { value, cooldownUntil, lastSurfacedAt } = this._state;
		const cfg = this._config;

		// ── 4. Skip all accumulation during cooldown ──────────────────────────
		if (cooldownUntil !== null && now < cooldownUntil) {
			this._wasNoveltySpike = false;
			return;
		}

		const v = beauState.personalityVector;
		let accumulation = 0;
		let dominantTrigger = 'none';

		// ── 1. Accumulate from vector magnitude ───────────────────────────────
		const magnitude = Math.sqrt(v.wonder ** 2 + v.reflection ** 2 + v.mischief ** 2);
		const magnitudeContrib = magnitude * cfg.magnitudeRate;
		if (magnitudeContrib > 0) {
			accumulation += magnitudeContrib;
			dominantTrigger = 'vector_magnitude';
		}

		// ── 2. Accumulate from time since last surfaced ───────────────────────
		let timeContrib = 0;
		if (lastSurfacedAt !== null)
updateBaseline method · typescript · L178-L204 (27 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	updateBaseline(sensor: string, value: number): void {
		const { baselines, baselineInitialized } = this._state;
		const cfg = this._config;

		if (!baselineInitialized[sensor]) {
			// Initialize baseline from first reading (not zero)
			baselines[sensor] = value;
			baselineInitialized[sensor] = true;
			return;
		}

		// EMA update
		const prev = baselines[sensor];
		const updated = (1 - cfg.baselineAlpha) * prev + cfg.baselineAlpha * value;
		baselines[sensor] = updated;

		// Novelty score — use floor from NOVELTY_MIN_BASELINES or 1.0
		const floor = NOVELTY_MIN_BASELINES[sensor] ?? 1.0;
		const divisor = Math.max(prev, floor);
		const score = Math.abs(value - prev) / divisor;

		if (score > NOVELTY_DEVIATION_THRESHOLD) {
			// Add spike proportional to deviation, capped at 0.2
			const spike = Math.min(0.2, score * 0.1);
			this._state.value = Math.min(1, this._state.value + spike);
		}
	}
shouldDispatch method · typescript · L215-L235 (21 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	shouldDispatch(
		dailyBudget: DailyBudgetStatus,
		rng: () => number = Math.random,
	): boolean {
		const now = Date.now();
		const { value, cooldownUntil } = this._state;

		// Block if in cooldown
		if (cooldownUntil !== null && now < cooldownUntil) {
			return false;
		}

		// Block if daily budget exhausted
		if (dailyBudget.atTotalCap) {
			return false;
		}

		// Compare against randomized threshold
		const threshold = BASE_THRESHOLD + rng() * THRESHOLD_VARIANCE;
		return value > threshold;
	}
resetAfterDispatch method · typescript · L243-L247 (5 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	resetAfterDispatch(): void {
		this._state.value = this._state.value * (1 - this._config.dispatchReduction);
		// Note: cooldown is set by notifySurfaced() when the thought is actually shown to the user.
		// resetAfterDispatch is called at dispatch time (request sent), not surface time.
	}
Citation: Repobility (2026). State of AI-Generated Code. https://repobility.com/research/
notifySurfaced method · typescript · L253-L257 (5 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	notifySurfaced(): void {
		const now = Date.now();
		this._state.lastSurfacedAt = now;
		this._state.cooldownUntil = now + COOLDOWN_MS;
	}
getValue method · typescript · L262-L264 (3 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	getValue(): number {
		return this._state.value;
	}
getLastTrigger method · typescript · L270-L272 (3 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	getLastTrigger(): string {
		return this._lastTrigger;
	}
wasNoveltySpike method · typescript · L275-L277 (3 LOC)
beau-terminal/src/lib/server/thoughts/pressure.ts
	wasNoveltySpike(): boolean {
		return this._wasNoveltySpike;
	}
constructor method · typescript · L71-L76 (6 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	constructor(db: BetterSQLite3Database<any> | null) {
		this.db = db;
		if (db) {
			this._loadFromDb();
		}
	}
enqueue method · typescript · L86-L121 (36 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	enqueue(opts: EnqueueOpts): PendingThought | null {
		const thought: PendingThought = {
			id: opts.id,
			type: opts.type,
			trigger: opts.trigger,
			text: null,
			status: 'requested',
			priority: PRIORITY[opts.type],
			contextJson: opts.contextJson,
			createdAt: new Date().toISOString(),
			generatedAt: null,
			surfacedAt: null,
			expiresAt: opts.expiresAt,
			novelty: opts.novelty,
			model: null,
			generationMs: null,
			traceId: null,
		};

		if (this.thoughts.size >= MAX_QUEUE_SIZE) {
			// Find the lowest-priority thought currently in the queue
			const lowestExisting = this._findLowestPriority();
			if (lowestExisting && lowestExisting.priority < thought.priority) {
				// Drop the lowest existing to make room
				this._updateStatus(lowestExisting.id, 'dropped');
				this.thoughts.delete(lowestExisting.id);
			} else {
				// New thought has same or lower priority — reject it
				return null;
			}
		}

		this.thoughts.set(thought.id, thought);
		this._dbUpsert(thought
receiveResult method · typescript · L129-L148 (20 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	receiveResult(result: ThoughtResult): void {
		const thought = this.thoughts.get(result.id);
		if (!thought) return;

		if (result.text === null) {
			thought.status = 'dropped';
			this._dbUpdate(thought);
			recordFeedback({ traceId: thought.traceId, requestId: thought.id, reviewer: 'system', outcomeType: 'dropped' });
			return;
		}

		thought.text = result.text;
		thought.generatedAt = result.generatedAt;
		thought.model = result.model;
		thought.generationMs = result.generationMs;
		thought.status = 'pending';
		this._dbUpdate(thought);

		this._promoteReady();
	}
getReady method · typescript · L151-L156 (6 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	getReady(): PendingThought | null {
		for (const t of this.thoughts.values()) {
			if (t.status === 'ready') return t;
		}
		return null;
	}
Repobility — same analyzer, your code, free for public repos · /scan/
surface method · typescript · L162-L171 (10 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	surface(): PendingThought | null {
		const thought = this.getReady();
		if (!thought) return null;

		thought.status = 'surfaced';
		thought.surfacedAt = new Date().toISOString();
		this._dbUpdate(thought);
		recordFeedback({ traceId: thought.traceId, requestId: thought.id, reviewer: 'system', outcomeType: 'surfaced' });
		return thought;
	}
runDecay method · typescript · L178-L203 (26 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	runDecay(): void {
		const now = Date.now();

		for (const thought of this.thoughts.values()) {
			// Skip terminal statuses
			if (!ACTIVE_STATUSES.has(thought.status)) continue;

			// Check TTL expiry
			if (new Date(thought.expiresAt).getTime() <= now) {
				thought.status = 'decayed';
				this._dbUpdate(thought);
				recordFeedback({ traceId: thought.traceId, requestId: thought.id, reviewer: 'system', outcomeType: 'decayed' });
				continue;
			}

			// Check generation timeout for 'requested' state
			if (thought.status === 'requested') {
				const age = now - new Date(thought.createdAt).getTime();
				if (age > GENERATION_TIMEOUT_MS) {
					thought.status = 'dropped';
					this._dbUpdate(thought);
					recordFeedback({ traceId: thought.traceId, requestId: thought.id, reviewer: 'system', outcomeType: 'dropped' });
				}
			}
		}
	}
getDailyBudgetStatus method · typescript · L209-L264 (56 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	getDailyBudgetStatus(): DailyBudgetStatus {
		if (!this.db) {
			// In-memory fallback: count surfaced thoughts from today (UTC)
			const todayPrefix = new Date().toISOString().slice(0, 10);
			let surfacedToday = 0;
			let haikuToday = 0;
			let selfReflectionToday = 0;
			for (const t of this.thoughts.values()) {
				if (t.status === 'surfaced' && t.surfacedAt?.startsWith(todayPrefix)) {
					surfacedToday++;
					if (t.type === 'haiku') haikuToday++;
					if (t.type === 'self-reflection') selfReflectionToday++;
				}
			}
			return {
				surfacedToday,
				haikuToday,
				selfReflectionToday,
				atHaikuCap: haikuToday >= MAX_DAILY_HAIKU,
				atSelfReflectionCap: selfReflectionToday >= MAX_DAILY_SELF_REFLECTIONS,
				atTotalCap: surfacedToday >= MAX_DAILY_THOUGHTS,
			};
		}

		// DB-backed: use datetime(surfaced_at, 'localtime') for Lafayette timezone
		const rows = this.db
			.select({
				type: pendingThoughts.type,
				count: sql<number>`count(*)`,
			})
			.from(pendingThought
getReadyThoughtType method · typescript · L267-L269 (3 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	getReadyThoughtType(): ThoughtType | null {
		return this.getReady()?.type ?? null;
	}
pendingCount method · typescript · L272-L278 (7 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	pendingCount(): number {
		let count = 0;
		for (const t of this.thoughts.values()) {
			if (ACTIVE_STATUSES.has(t.status)) count++;
		}
		return count;
	}
getByStatus method · typescript · L281-L283 (3 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	getByStatus(status: ThoughtStatus): PendingThought[] {
		return Array.from(this.thoughts.values()).filter(t => t.status === status);
	}
get method · typescript · L286-L288 (3 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	get(id: string): PendingThought | undefined {
		return this.thoughts.get(id);
	}
has method · typescript · L291-L293 (3 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	has(id: string): boolean {
		return this.thoughts.has(id);
	}
Repobility analyzer · published findings · https://repobility.com
size method · typescript · L296-L298 (3 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	size(): number {
		return this.thoughts.size;
	}
setTraceId method · typescript · L301-L306 (6 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	setTraceId(thoughtId: string, traceId: string): void {
		const thought = this.thoughts.get(thoughtId);
		if (thought) {
			thought.traceId = traceId;
		}
	}
hasTypeInQueue method · typescript · L309-L316 (8 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	hasTypeInQueue(type: ThoughtType): boolean {
		for (const t of this.thoughts.values()) {
			if (t.type === type && ['requested', 'generating', 'pending', 'ready'].includes(t.status)) {
				return true;
			}
		}
		return false;
	}
_promoteReady method · typescript · L328-L368 (41 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _promoteReady(): void {
		// Find the best pending candidate
		let bestPending: PendingThought | null = null;
		for (const t of this.thoughts.values()) {
			if (t.status !== 'pending') continue;
			if (!bestPending) {
				bestPending = t;
				continue;
			}
			if (t.priority > bestPending.priority) {
				bestPending = t;
			} else if (t.priority === bestPending.priority && t.createdAt > bestPending.createdAt) {
				bestPending = t;
			}
		}

		if (!bestPending) return; // nothing to promote

		const currentReady = this.getReady();

		if (!currentReady) {
			// Simple case — nothing ready yet
			bestPending.status = 'ready';
			this._dbUpdate(bestPending);
			return;
		}

		// Check if bestPending outranks currentReady
		const shouldSwap =
			bestPending.priority > currentReady.priority ||
			(bestPending.priority === currentReady.priority && bestPending.createdAt > currentReady.createdAt);

		if (shouldSwap) {
			// Demote currentReady back to pending
			currentReady.status = 
_findLowestPriority method · typescript · L371-L379 (9 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _findLowestPriority(): PendingThought | null {
		let lowest: PendingThought | null = null;
		for (const t of this.thoughts.values()) {
			if (!lowest || t.priority < lowest.priority) {
				lowest = t;
			}
		}
		return lowest;
	}
_loadFromDb method · typescript · L382-L411 (30 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _loadFromDb(): void {
		if (!this.db) return;
		try {
			const rows = this.db.select().from(pendingThoughts).all();
			for (const row of rows) {
				const thought: PendingThought = {
					id: row.id,
					type: row.type as ThoughtType,
					trigger: row.trigger,
					text: row.text ?? null,
					status: row.status as ThoughtStatus,
					priority: row.priority,
					contextJson: row.contextJson,
					createdAt: row.createdAt,
					generatedAt: row.generatedAt ?? null,
					surfacedAt: row.surfacedAt ?? null,
					expiresAt: row.expiresAt,
					novelty: row.novelty === 1,
					model: row.model ?? null,
					generationMs: row.generationMs ?? null,
					traceId: null, // traceId is runtime-only, not persisted in DB
				};
				this.thoughts.set(thought.id, thought);
			}
			// Promote best pending thought to ready after loading
			this._promoteReady();
		} catch {
			// DB not ready yet — will be populated on first enqueue
		}
	}
_dbUpsert method · typescript · L414-L446 (33 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _dbUpsert(thought: PendingThought): void {
		if (!this.db) return;
		try {
			this.db.insert(pendingThoughts).values({
				id: thought.id,
				type: thought.type,
				trigger: thought.trigger,
				text: thought.text ?? undefined,
				status: thought.status,
				priority: thought.priority,
				contextJson: thought.contextJson,
				createdAt: thought.createdAt,
				generatedAt: thought.generatedAt ?? undefined,
				surfacedAt: thought.surfacedAt ?? undefined,
				expiresAt: thought.expiresAt,
				novelty: thought.novelty ? 1 : 0,
				model: thought.model ?? undefined,
				generationMs: thought.generationMs ?? undefined,
			}).onConflictDoUpdate({
				target: pendingThoughts.id,
				set: {
					status: thought.status,
					text: thought.text ?? undefined,
					generatedAt: thought.generatedAt ?? undefined,
					surfacedAt: thought.surfacedAt ?? undefined,
					model: thought.model ?? undefined,
					generationMs: thought.generationMs ?? undefined,
				},
			}).run();
		} catch {
_dbUpdate method · typescript · L449-L463 (15 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _dbUpdate(thought: PendingThought): void {
		if (!this.db) return;
		try {
			this.db.update(pendingThoughts).set({
				status: thought.status,
				text: thought.text ?? undefined,
				generatedAt: thought.generatedAt ?? undefined,
				surfacedAt: thought.surfacedAt ?? undefined,
				model: thought.model ?? undefined,
				generationMs: thought.generationMs ?? undefined,
			}).where(eq(pendingThoughts.id, thought.id)).run();
		} catch {
			// Non-fatal
		}
	}
Repobility — the code-quality scanner for AI-generated software · https://repobility.com
_updateStatus method · typescript · L466-L472 (7 LOC)
beau-terminal/src/lib/server/thoughts/queue.ts
	private _updateStatus(id: string, status: ThoughtStatus): void {
		const thought = this.thoughts.get(id);
		if (thought) {
			thought.status = status;
			this._dbUpdate(thought);
		}
	}
classifyEligibility function · typescript · L20-L60 (41 LOC)
beau-terminal/src/lib/server/training/eligibility.ts
export function classifyEligibility(input: EligibilityInput): EligibilityResult {
  const hasPrivate = input.retrievedCollections.includes('beau_private');

  // Step 1: Consent scope
  let consentScope: ConsentScope;
  if (input.requestKind === 'manual.prompt') {
    consentScope = 'user_content';
  } else if (hasPrivate) {
    consentScope = 'mixed';
  } else {
    consentScope = 'beau_output';
  }

  // Step 2: Privacy class
  let privacyClass: PrivacyClass;
  if (hasPrivate || consentScope === 'mixed') {
    privacyClass = 'private';
  } else if (consentScope === 'user_content') {
    privacyClass = 'trusted';
  } else {
    privacyClass = 'public';
  }

  // Step 3: Training eligibility (system defaults — no policy overlay in Stage 0)
  let trainingEligibility: TrainingEligibility;
  let trainingEligibilityReason: string;

  if (privacyClass === 'private') {
    trainingEligibility = 'never';
    trainingEligibilityReason = 'contains private memory fragments';
  } else if (consent
recordFeedback function · typescript · L21-L34 (14 LOC)
beau-terminal/src/lib/server/training/feedback.ts
export function recordFeedback(input: FeedbackInput): void {
  try {
    db.insert(generationFeedback).values({
      traceId: input.traceId ?? null,
      requestId: input.requestId ?? null,
      reviewer: input.reviewer,
      outcomeType: input.outcomeType,
      finalText: input.finalText ?? null,
      notes: input.notes ?? null,
    }).run();
  } catch {
    // fail-open: feedback is best-effort, never blocks the thought pipeline
  }
}
writeTrace function · typescript · L18-L80 (63 LOC)
beau-terminal/src/lib/server/training/index.ts
function writeTrace(payload: TracePayload): void {
  // Atomic transaction: trace + all retrievals committed together.
  // If any insert fails, the entire write rolls back — no orphaned traces.
  const insertAll = db.transaction(() => {
  db.insert(generationTraces).values({
    traceId: payload.traceId,
    requestId: payload.requestId,
    parentTraceId: payload.parentTraceId,
    attemptNumber: payload.attemptNumber,
    requestKind: payload.requestKind,
    origin: payload.origin,
    tier: payload.tier,
    modelFamily: payload.modelFamily,
    modelName: payload.modelName,
    modelDigest: payload.modelDigest,
    generationParams: payload.generationParams ? JSON.stringify(payload.generationParams) : null,
    provider: payload.provider,
    runtime: payload.runtime,
    promptTemplateHash: payload.promptProvenance.templateHash,
    promptPolicyVersion: payload.promptProvenance.promptPolicyVersion,
    promptProfile: payload.promptProvenance.promptProfile,
    retrievalPolicyVer
updateTraceStatus function · typescript · L86-L91 (6 LOC)
beau-terminal/src/lib/server/training/index.ts
function updateTraceStatus(traceId: string, status: string): void {
  db.update(generationTraces)
    .set({ responseStatus: status })
    .where(eq(generationTraces.traceId, traceId))
    .run();
}
initTraining function · typescript · L97-L105 (9 LOC)
beau-terminal/src/lib/server/training/index.ts
export function initTraining(): void {
  if (outbox) return;
  outbox = new TraceOutbox({
    flushIntervalMs: 2000,
    writer: writeTrace,
    statusUpdater: updateTraceStatus,
  });
  outbox.start();
}
getTraceOutbox function · typescript · L111-L113 (3 LOC)
beau-terminal/src/lib/server/training/index.ts
export function getTraceOutbox(): TraceOutbox | null {
  return outbox;
}
_resetTrainingForTesting function · typescript · L116-L121 (6 LOC)
beau-terminal/src/lib/server/training/index.ts
export function _resetTrainingForTesting(): void {
  if (outbox) {
    outbox.stop();
  }
  outbox = null;
}
Citation: Repobility (2026). State of AI-Generated Code. https://repobility.com/research/
getActiveModelForTier function · typescript · L12-L16 (5 LOC)
beau-terminal/src/lib/server/training/model-registry.ts
export function getActiveModelForTier(tier: string) {
  return db.select().from(llmModelVariants)
    .where(and(eq(llmModelVariants.tier, tier), eq(llmModelVariants.status, 'active')))
    .get() ?? null;
}
getAllModelVariants function · typescript · L21-L23 (3 LOC)
beau-terminal/src/lib/server/training/model-registry.ts
export function getAllModelVariants() {
  return db.select().from(llmModelVariants).all();
}
getModelVariantById function · typescript · L28-L32 (5 LOC)
beau-terminal/src/lib/server/training/model-registry.ts
export function getModelVariantById(id: number) {
  return db.select().from(llmModelVariants)
    .where(eq(llmModelVariants.id, id))
    .get() ?? null;
}
assembleTracePayload function · typescript · L32-L78 (47 LOC)
beau-terminal/src/lib/server/training/trace-capture.ts
export function assembleTracePayload(ctx: TraceContext): TracePayload {
  const collections = ctx.prepareResult.retrievals.map((r) => r.collection);
  const eligibility = classifyEligibility({
    requestKind: ctx.request.kind,
    retrievedCollections: collections,
  });

  // Extract family from the colon-delimited model tag (e.g. "gemma3:4b" → "gemma3")
  const modelFamily = ctx.model.split(':')[0] ?? 'unknown';

  // contextMode is only meaningful for thought.generate requests
  const contextMode =
    ctx.request.kind === 'thought.generate'
      ? (ctx.request.input.context?.mode ?? null)
      : null;

  return {
    traceId: nanoid(16),
    requestId: ctx.request.requestId,
    parentTraceId: ctx.parentTraceId,
    attemptNumber: ctx.attemptNumber,
    requestKind: ctx.request.kind,
    origin: ctx.request.origin,
    tier: ctx.plan.targetTier,
    modelFamily,
    modelName: ctx.model,
    modelDigest: null,
    generationParams: null,
    provider: 'ollama',
    runtime: null
TraceOutbox class · typescript · L15-L94 (80 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
export class TraceOutbox {
  private queue: TracePayload[] = [];
  private interval: ReturnType<typeof setInterval> | null = null;
  private config: TraceOutboxConfig;
  private lastEnqueuedTraceId: string | null = null;

  constructor(config: TraceOutboxConfig = {}) {
    this.config = config;
  }

  get pending(): number { return this.queue.length; }
  get running(): boolean { return this.interval !== null; }

  enqueue(payload: TracePayload): void {
    this.queue.push(payload);
    this.lastEnqueuedTraceId = payload.traceId;
  }

  /** Returns the traceId of the most recently enqueued payload, or null. */
  getLastTraceId(): string | null {
    return this.lastEnqueuedTraceId;
  }

  flush(): void {
    if (this.queue.length === 0) return;
    const batch = [...this.queue];
    const flushed: number[] = [];

    for (let i = 0; i < batch.length; i++) {
      try {
        if (this.config.writer) {
          this.config.writer(batch[i]);
        }
        flushed.push(i);
      } ca
constructor method · typescript · L21-L23 (3 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  constructor(config: TraceOutboxConfig = {}) {
    this.config = config;
  }
enqueue method · typescript · L28-L31 (4 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  enqueue(payload: TracePayload): void {
    this.queue.push(payload);
    this.lastEnqueuedTraceId = payload.traceId;
  }
getLastTraceId method · typescript · L34-L36 (3 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  getLastTraceId(): string | null {
    return this.lastEnqueuedTraceId;
  }
Repobility — same analyzer, your code, free for public repos · /scan/
flush method · typescript · L38-L58 (21 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  flush(): void {
    if (this.queue.length === 0) return;
    const batch = [...this.queue];
    const flushed: number[] = [];

    for (let i = 0; i < batch.length; i++) {
      try {
        if (this.config.writer) {
          this.config.writer(batch[i]);
        }
        flushed.push(i);
      } catch {
        // fail-open: skip this entry, try next flush
      }
    }

    // Remove successfully flushed entries in reverse order to preserve indices
    for (let i = flushed.length - 1; i >= 0; i--) {
      this.queue.splice(flushed[i], 1);
    }
  }
updateStatus method · typescript · L63-L80 (18 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  updateStatus(traceId: string, responseStatus: string): boolean {
    // Try in-memory first (not yet flushed)
    const entry = this.queue.find(p => p.traceId === traceId);
    if (entry) {
      entry.responseStatus = responseStatus;
      return true;
    }
    // Fallback: already flushed to DB
    if (this.config.statusUpdater) {
      try {
        this.config.statusUpdater(traceId, responseStatus);
        return true;
      } catch {
        return false;
      }
    }
    return false;
  }
start method · typescript · L82-L85 (4 LOC)
beau-terminal/src/lib/server/training/trace-outbox.ts
  start(): void {
    if (this.interval) return;
    this.interval = setInterval(() => this.flush(), this.config.flushIntervalMs ?? 2000);
  }
‹ prevpage 6 / 9next ›