Build & Deploy / build-and-push (discord-gateway) (push) Failing after 2m22s
Build & Deploy / build-and-push (backend) (push) Failing after 3m22s
Build & Deploy / build-and-push (proxy) (push) Successful in 1m36s
Build & Deploy / deploy (push) Skipped
- Consolidate all DB schema definitions into packages/shared as single source of truth - Migrate backend from raw SQL to Drizzle ORM across all modules - Extract frontend inline UI into separate component files - Refactor discord-gateway circuitBreaker into conversationState + moderationState - Convert messageStore to Proxy singleton pattern - Add validateBody/validateQuery middleware + Zod schemas for API endpoints - Modernize Docker builds with multi-stage + pnpm deploy - Migrate CI/CD from deployment to image-based pipeline - Remove 60+ unused/dead files (~15K lines) - Update color scheme from sky-blue to teal-cyan - Move DB connection management to @bete/shared/database Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
134 lines
4.4 KiB
TypeScript
134 lines
4.4 KiB
TypeScript
import { createChildLogger } from "@bete/shared/logger";
|
|
import { config } from "../../shared/config/config.js";
|
|
import { messageStore } from "../message-capture/messageStore.js";
|
|
import type { MessageRecord } from "../message-capture/types.js";
|
|
import {
|
|
pickBatchWithinBudget,
|
|
processBatch,
|
|
skipAgeRestrictedMessages,
|
|
} from "./batchProcessor.js";
|
|
import {
|
|
conversationConsecutiveErrors,
|
|
conversationDebounceTimers,
|
|
conversationErrorCooldown,
|
|
conversationProcessing,
|
|
isConversationProcessingLocked,
|
|
MAX_CONSECUTIVE_ERRORS,
|
|
} from "./conversationState.js";
|
|
|
|
const logger = createChildLogger("batch-scheduler");
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Scheduling
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Schedules a debounced analysis run for a conversation.
|
|
*
|
|
* The async work inside setTimeout is wrapped in an explicit .catch() so
|
|
* DB errors don't produce unhandled promise rejections. Uses a unified
|
|
* single-timer path: always clear-and-reset one timer per conversation key
|
|
* regardless of whether a cooldown is active.
|
|
*/
|
|
export function scheduleConversationAnalysis(conversationKey: string): void {
|
|
if (isConversationProcessingLocked(conversationKey)) {
|
|
return;
|
|
}
|
|
|
|
const convoCooldown = conversationErrorCooldown.get(conversationKey) ?? 0;
|
|
const convoErrors = conversationConsecutiveErrors.get(conversationKey) ?? 0;
|
|
|
|
// Hard-block: circuit breaker threshold reached AND cooldown still active.
|
|
if (convoErrors >= MAX_CONSECUTIVE_ERRORS && Date.now() < convoCooldown) {
|
|
return;
|
|
}
|
|
|
|
// Unified delay: honour the cooldown window if active, otherwise use the
|
|
// normal debounce interval. Always clear-and-reset so only ONE timer is
|
|
// ever pending per conversation key regardless of call source.
|
|
const now = Date.now();
|
|
const delayMs =
|
|
convoCooldown > now
|
|
? convoCooldown - now + 500
|
|
: config.AI_ANALYSIS_DEBOUNCE_MS;
|
|
|
|
const existingTimer = conversationDebounceTimers.get(conversationKey);
|
|
if (existingTimer) {
|
|
clearTimeout(existingTimer);
|
|
}
|
|
|
|
const timer = setTimeout(() => {
|
|
conversationDebounceTimers.delete(conversationKey);
|
|
|
|
if (isConversationProcessingLocked(conversationKey)) {
|
|
return;
|
|
}
|
|
const processingStartedAt = Date.now();
|
|
conversationProcessing.set(conversationKey, processingStartedAt);
|
|
|
|
messageStore
|
|
.getPendingMessagesByConversation(
|
|
conversationKey,
|
|
config.AI_ANALYSIS_MAX_BATCH_SIZE,
|
|
)
|
|
.then(async (messages: MessageRecord[]) => {
|
|
if (messages.length === 0) {
|
|
if (
|
|
conversationProcessing.get(conversationKey) === processingStartedAt
|
|
) {
|
|
conversationProcessing.delete(conversationKey);
|
|
}
|
|
return;
|
|
}
|
|
|
|
const processableMessages = await skipAgeRestrictedMessages(messages);
|
|
if (processableMessages.length === 0) {
|
|
if (
|
|
conversationProcessing.get(conversationKey) === processingStartedAt
|
|
) {
|
|
conversationProcessing.delete(conversationKey);
|
|
}
|
|
return;
|
|
}
|
|
|
|
let trimmed = pickBatchWithinBudget(
|
|
processableMessages,
|
|
config.AI_ANALYSIS_MAX_TARGET_TOKENS,
|
|
50,
|
|
);
|
|
|
|
// If every message individually exceeds the token budget,
|
|
// fall back to the first message alone to avoid stuck-pending deadlock.
|
|
if (trimmed.length === 0 && processableMessages.length > 0) {
|
|
trimmed = processableMessages.slice(0, 1);
|
|
logger.warn(
|
|
{
|
|
conversationKey,
|
|
messageId: processableMessages[0]?.id,
|
|
tokenBudget: config.AI_ANALYSIS_MAX_TARGET_TOKENS,
|
|
},
|
|
"All messages exceed token budget -- processing first message alone to avoid stuck-pending deadlock",
|
|
);
|
|
}
|
|
|
|
return processBatch(conversationKey, trimmed, processingStartedAt);
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (
|
|
conversationProcessing.get(conversationKey) === processingStartedAt
|
|
) {
|
|
conversationProcessing.delete(conversationKey);
|
|
}
|
|
logger.error(
|
|
{
|
|
conversationKey,
|
|
error: err instanceof Error ? err.message : String(err),
|
|
},
|
|
"Failed to fetch or dispatch pending messages for scheduled analysis",
|
|
);
|
|
});
|
|
}, delayMs);
|
|
|
|
conversationDebounceTimers.set(conversationKey, timer);
|
|
}
|