refactor(runtime): enforce source file size boundaries
This commit is contained in:
1 parent
212e03219d
commit
3aaa64ddfd
14 files changed
+1147
-1284
No files matched your search
@@ -0,0 +1,75 @@
|
||||
import type { AgentMessageRecord } from "@shared/types";
|
||||
|
||||
export interface InboundCorrelationEntry {
|
||||
fromAgentId: string;
|
||||
senderKind: AgentMessageRecord["senderKind"];
|
||||
messageId: string;
|
||||
/**
|
||||
* Stable pi-message timestamp that the projected user turn carries.
|
||||
* Reverse-indexed so `message_end` can resolve back to `messageId`.
|
||||
*/
|
||||
piTimestamp: number;
|
||||
}
|
||||
|
||||
interface InboundCorrelationRegistry {
|
||||
byMessageId: Map<string, InboundCorrelationEntry>;
|
||||
byPiTimestamp: Map<number, string>;
|
||||
lastAllocatedTimestamp: number;
|
||||
}
|
||||
|
||||
const MAX_INBOUND_CORRELATIONS_PER_RUN = 512;
|
||||
|
||||
export class InboundCorrelationRegistryStore {
|
||||
private readonly registries = new Map<string, InboundCorrelationRegistry>();
|
||||
|
||||
register(runId: string, record: AgentMessageRecord, baselineTimestamp: number): number {
|
||||
const registry = this.getOrCreate(runId);
|
||||
const allocated = Math.max(baselineTimestamp, registry.lastAllocatedTimestamp + 1);
|
||||
registry.lastAllocatedTimestamp = allocated;
|
||||
const entry: InboundCorrelationEntry = {
|
||||
fromAgentId: record.fromAgentId,
|
||||
senderKind: record.senderKind,
|
||||
messageId: record.id,
|
||||
piTimestamp: allocated,
|
||||
};
|
||||
registry.byMessageId.set(record.id, entry);
|
||||
registry.byPiTimestamp.set(allocated, record.id);
|
||||
this.evictOldestIfNeeded(registry);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
lookup(runId: string, timestamp: number | undefined): InboundCorrelationEntry | null {
|
||||
if (typeof timestamp !== "number" || !Number.isFinite(timestamp)) return null;
|
||||
const registry = this.registries.get(runId);
|
||||
if (!registry) return null;
|
||||
const messageId = registry.byPiTimestamp.get(timestamp);
|
||||
if (!messageId) return null;
|
||||
return registry.byMessageId.get(messageId) ?? null;
|
||||
}
|
||||
|
||||
deleteRun(runId: string): void {
|
||||
this.registries.delete(runId);
|
||||
}
|
||||
|
||||
private getOrCreate(runId: string): InboundCorrelationRegistry {
|
||||
const existing = this.registries.get(runId);
|
||||
if (existing) return existing;
|
||||
const registry: InboundCorrelationRegistry = {
|
||||
byMessageId: new Map(),
|
||||
byPiTimestamp: new Map(),
|
||||
lastAllocatedTimestamp: 0,
|
||||
};
|
||||
this.registries.set(runId, registry);
|
||||
return registry;
|
||||
}
|
||||
|
||||
private evictOldestIfNeeded(registry: InboundCorrelationRegistry): void {
|
||||
while (registry.byMessageId.size > MAX_INBOUND_CORRELATIONS_PER_RUN) {
|
||||
const oldestMessageId = registry.byMessageId.keys().next().value;
|
||||
if (!oldestMessageId) break;
|
||||
const entry = registry.byMessageId.get(oldestMessageId);
|
||||
registry.byMessageId.delete(oldestMessageId);
|
||||
if (entry) registry.byPiTimestamp.delete(entry.piTimestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user