402 lines
15 KiB
TypeScript
402 lines
15 KiB
TypeScript
import { createHash } from "node:crypto";
|
|
|
|
export interface InitiativeParticipant {
|
|
identityId: string;
|
|
participantKind: "repo_face" | "native_persona" | "system_agent";
|
|
turnKind: "repo_face_rumination" | "void_moderation";
|
|
repoName: string;
|
|
displayName: string;
|
|
initiativeSpeed: number;
|
|
reactionBias: number;
|
|
interruptThreshold: number;
|
|
groups: string[];
|
|
heat: number;
|
|
dynamicHeat: number;
|
|
responsePressure: number;
|
|
responsePressureEvidence: Array<{
|
|
messageId: string;
|
|
observedAt: string;
|
|
similarity: number;
|
|
contribution: number;
|
|
}>;
|
|
semanticInterruptReceipts: string[];
|
|
effectiveSpeed: number;
|
|
baseRecoveryMinutes: number;
|
|
status: "active" | "blocked" | "withdrawn" | "offscreen";
|
|
currentLoad: number;
|
|
nextTurnAt: number;
|
|
lastTurnAt?: number;
|
|
activeTurnStartedAt?: number;
|
|
activeJobId?: string;
|
|
lastQueuedAt?: string;
|
|
queuedCount: number;
|
|
constraints: string[];
|
|
}
|
|
|
|
export interface ParticipantSpec {
|
|
id: string;
|
|
participantKind: InitiativeParticipant["participantKind"];
|
|
turnKind: InitiativeParticipant["turnKind"];
|
|
repoName: string;
|
|
displayName: string;
|
|
allowedChannelIds: string[];
|
|
channelSpeedMultiplier: number;
|
|
}
|
|
|
|
export interface PendingMention {
|
|
identityId: string;
|
|
}
|
|
|
|
export interface InitiativeState<TParticipant extends InitiativeParticipant = InitiativeParticipant> {
|
|
initiativeClock: number;
|
|
lastTickAt?: string;
|
|
participants: TParticipant[];
|
|
pendingMentions: PendingMention[];
|
|
history: Array<Record<string, unknown>>;
|
|
}
|
|
|
|
export interface RestSnapshot {
|
|
isNapping: boolean;
|
|
}
|
|
|
|
export interface IdleCoolingPolicy {
|
|
enabled: boolean;
|
|
active: boolean;
|
|
nextUnpromptedTurnAllowedAt?: string;
|
|
}
|
|
|
|
export interface ReconcileParticipantsInput<TSpec extends ParticipantSpec = ParticipantSpec> {
|
|
existing: InitiativeParticipant[];
|
|
specs: TSpec[];
|
|
defaultChannelId?: string;
|
|
speedOverrides: Record<string, number>;
|
|
heatOverrides: Record<string, number>;
|
|
initiativeClock: number;
|
|
baseRecoveryMinutes: number;
|
|
globalHeat: number;
|
|
}
|
|
|
|
export function reconcileParticipants(input: ReconcileParticipantsInput): InitiativeParticipant[] {
|
|
const existingById = new Map(input.existing.map((entry) => [entry.identityId, entry]));
|
|
const count = Math.max(input.specs.length, 1);
|
|
|
|
return input.specs.map((spec, index) => {
|
|
const current = existingById.get(spec.id);
|
|
const hasChannel = spec.participantKind === "system_agent" || Boolean(spec.allowedChannelIds[0] || input.defaultChannelId);
|
|
const speed = initiativeSpeedFor(spec, input.speedOverrides) * spec.channelSpeedMultiplier;
|
|
const groups = initiativeGroupsFor(spec);
|
|
const heat = heatFor(spec, groups, input.globalHeat, input.heatOverrides);
|
|
const dynamicHeat = Number.isFinite(current?.dynamicHeat) ? current!.dynamicHeat : 1;
|
|
const effectiveSpeed = clamp(speed * heat * dynamicHeat, 0.1, 12);
|
|
const nextTurnAt = Number.isFinite(current?.nextTurnAt)
|
|
? current!.nextTurnAt
|
|
: input.initiativeClock + ((input.baseRecoveryMinutes / count) * index);
|
|
|
|
if (current) {
|
|
return {
|
|
...current,
|
|
participantKind: spec.participantKind,
|
|
turnKind: spec.turnKind,
|
|
repoName: spec.repoName,
|
|
displayName: spec.displayName,
|
|
initiativeSpeed: speed,
|
|
groups,
|
|
heat,
|
|
dynamicHeat,
|
|
responsePressure: Number.isFinite(current.responsePressure) ? current.responsePressure : 0,
|
|
responsePressureEvidence: Array.isArray(current.responsePressureEvidence) ? current.responsePressureEvidence : [],
|
|
semanticInterruptReceipts: Array.isArray(current.semanticInterruptReceipts) ? current.semanticInterruptReceipts : [],
|
|
effectiveSpeed,
|
|
baseRecoveryMinutes: input.baseRecoveryMinutes,
|
|
nextTurnAt,
|
|
constraints: mergeStrings(
|
|
mergeStrings(current.constraints, "Agent runtime uses CTB-style turns."),
|
|
"Wall-clock elapsed time advances initiative; heat changes recovery speed but does not fast-forward time.",
|
|
),
|
|
status: hasChannel
|
|
? current.status === "withdrawn" || current.status === "blocked" ? current.status : "active"
|
|
: "blocked",
|
|
};
|
|
}
|
|
|
|
return {
|
|
identityId: spec.id,
|
|
participantKind: spec.participantKind,
|
|
turnKind: spec.turnKind,
|
|
repoName: spec.repoName,
|
|
displayName: spec.displayName,
|
|
initiativeSpeed: speed,
|
|
reactionBias: reactionBiasFor(spec),
|
|
interruptThreshold: interruptThresholdFor(spec),
|
|
currentLoad: 0,
|
|
status: hasChannel ? "active" : "blocked",
|
|
groups,
|
|
heat,
|
|
dynamicHeat: 1,
|
|
responsePressure: 0,
|
|
responsePressureEvidence: [],
|
|
semanticInterruptReceipts: [],
|
|
effectiveSpeed,
|
|
baseRecoveryMinutes: input.baseRecoveryMinutes,
|
|
nextTurnAt,
|
|
queuedCount: 0,
|
|
constraints: [
|
|
"Agent runtime uses CTB-style turns.",
|
|
"Wall-clock elapsed time advances initiative; heat changes recovery speed but does not fast-forward time.",
|
|
"Worker final summaries are not auto-posted as the base bot.",
|
|
],
|
|
};
|
|
});
|
|
}
|
|
|
|
export function applyActiveTurnFreeze<TParticipant extends InitiativeParticipant>(
|
|
participant: TParticipant,
|
|
activeJobId: string | undefined,
|
|
state: InitiativeState<TParticipant>,
|
|
completedThisTick: Set<string>,
|
|
): TParticipant {
|
|
if (activeJobId) {
|
|
return {
|
|
...participant,
|
|
currentLoad: 1,
|
|
activeJobId,
|
|
activeTurnStartedAt: participant.activeTurnStartedAt ?? participant.lastTurnAt ?? state.initiativeClock,
|
|
};
|
|
}
|
|
|
|
if (participant.currentLoad >= 1 || participant.activeTurnStartedAt !== undefined || participant.activeJobId) {
|
|
const completedTurnStartedAt = participant.activeTurnStartedAt ?? participant.lastTurnAt ?? state.initiativeClock;
|
|
const unfrozen = {
|
|
...participant,
|
|
currentLoad: 0,
|
|
activeTurnStartedAt: undefined,
|
|
activeJobId: undefined,
|
|
};
|
|
const recoveryMinutes = recoveryFor(unfrozen);
|
|
unfrozen.nextTurnAt = Math.max(state.initiativeClock, completedTurnStartedAt) + recoveryMinutes;
|
|
completedThisTick.add(participant.identityId);
|
|
state.history.push({
|
|
type: "turn_completed",
|
|
identityId: participant.identityId,
|
|
completedAtClock: state.initiativeClock,
|
|
startedAtClock: completedTurnStartedAt,
|
|
nextTurnAt: unfrozen.nextTurnAt,
|
|
recoveryMinutes,
|
|
heat: participant.heat,
|
|
effectiveSpeed: participant.effectiveSpeed,
|
|
});
|
|
return unfrozen;
|
|
}
|
|
|
|
return { ...participant, currentLoad: 0 };
|
|
}
|
|
|
|
export function recordDryRunSelection(
|
|
participant: InitiativeParticipant,
|
|
state: InitiativeState,
|
|
queuedAt: string,
|
|
pendingMentionCount: number,
|
|
): void {
|
|
const recoveryMinutes = recoveryFor(participant);
|
|
participant.lastQueuedAt = queuedAt;
|
|
participant.lastTurnAt = state.initiativeClock;
|
|
participant.queuedCount += 1;
|
|
participant.nextTurnAt = Math.max(state.initiativeClock, participant.nextTurnAt) + recoveryMinutes;
|
|
state.history.push({
|
|
type: "dry_run_selected",
|
|
identityId: participant.identityId,
|
|
queuedAt,
|
|
initiativeClock: state.initiativeClock,
|
|
nextTurnAt: participant.nextTurnAt,
|
|
recoveryMinutes,
|
|
heat: participant.heat,
|
|
dynamicHeat: participant.dynamicHeat,
|
|
responsePressure: participant.responsePressure,
|
|
effectiveSpeed: participant.effectiveSpeed,
|
|
pendingMentionCount,
|
|
});
|
|
}
|
|
|
|
export function recordTurnStarted(input: {
|
|
participant: InitiativeParticipant;
|
|
state: InitiativeState;
|
|
queuedAt: string;
|
|
activeJobId?: string;
|
|
requestMessageId?: string;
|
|
pendingMentionCount: number;
|
|
}): void {
|
|
const { participant, state } = input;
|
|
participant.lastQueuedAt = input.queuedAt;
|
|
participant.activeTurnStartedAt = state.initiativeClock;
|
|
participant.activeJobId = input.activeJobId;
|
|
participant.lastTurnAt = state.initiativeClock;
|
|
participant.queuedCount += 1;
|
|
participant.currentLoad = 1;
|
|
state.history.push({
|
|
type: "queued",
|
|
identityId: participant.identityId,
|
|
participantKind: participant.participantKind,
|
|
turnKind: participant.turnKind,
|
|
activeJobId: input.activeJobId,
|
|
requestMessageId: input.requestMessageId,
|
|
queuedAt: input.queuedAt,
|
|
initiativeClock: state.initiativeClock,
|
|
frozen: true,
|
|
heat: participant.heat,
|
|
dynamicHeat: participant.dynamicHeat,
|
|
responsePressure: participant.responsePressure,
|
|
effectiveSpeed: participant.effectiveSpeed,
|
|
pendingMentionCount: input.pendingMentionCount,
|
|
});
|
|
}
|
|
|
|
export function recordTurnFailedToStart(input: {
|
|
participant: InitiativeParticipant;
|
|
state: InitiativeState;
|
|
queuedAt: string;
|
|
activeJobId?: string;
|
|
requestMessageId?: string;
|
|
reason: string;
|
|
}): void {
|
|
input.participant.currentLoad = 0;
|
|
input.state.history.push({
|
|
type: "turn_failed_to_start",
|
|
identityId: input.participant.identityId,
|
|
participantKind: input.participant.participantKind,
|
|
turnKind: input.participant.turnKind,
|
|
activeJobId: input.activeJobId,
|
|
requestMessageId: input.requestMessageId,
|
|
queuedAt: input.queuedAt,
|
|
initiativeClock: input.state.initiativeClock,
|
|
reason: input.reason,
|
|
heat: input.participant.heat,
|
|
dynamicHeat: input.participant.dynamicHeat,
|
|
responsePressure: input.participant.responsePressure,
|
|
effectiveSpeed: input.participant.effectiveSpeed,
|
|
});
|
|
}
|
|
|
|
export function advanceInitiativeClockFromWallClock(state: InitiativeState, now: Date): void {
|
|
const lastTickMs = Date.parse(state.lastTickAt ?? "");
|
|
if (!Number.isFinite(lastTickMs)) return;
|
|
const elapsedMinutes = (now.getTime() - lastTickMs) / 60_000;
|
|
if (elapsedMinutes <= 0) return;
|
|
state.initiativeClock = round3(state.initiativeClock + Math.min(elapsedMinutes, 60));
|
|
}
|
|
|
|
export function rescheduleStaleOverdueParticipants(state: InitiativeState): void {
|
|
const active = state.participants.filter((participant) => participant.status === "active");
|
|
const count = Math.max(active.length, 1);
|
|
let rescheduledCount = 0;
|
|
active.forEach((participant, index) => {
|
|
const staleThreshold = Math.max(participant.baseRecoveryMinutes, 15);
|
|
if (participant.nextTurnAt >= state.initiativeClock - staleThreshold) return;
|
|
participant.nextTurnAt = round3(state.initiativeClock + (participant.baseRecoveryMinutes / count) * index);
|
|
rescheduledCount += 1;
|
|
});
|
|
if (rescheduledCount > 0) {
|
|
state.history.push({ type: "wall_clock_resync", rescheduledCount, initiativeClock: state.initiativeClock });
|
|
}
|
|
}
|
|
|
|
export function applyPendingMentionPriority(state: InitiativeState): void {
|
|
const counts = countPendingMentionsByIdentity(state.pendingMentions);
|
|
for (const participant of state.participants) {
|
|
if (participant.status === "active" && participant.currentLoad < 1 && (counts.get(participant.identityId) ?? 0) > 0) {
|
|
participant.nextTurnAt = Math.min(participant.nextTurnAt, state.initiativeClock);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function countPendingMentionsByIdentity(pendingMentions: PendingMention[]): Map<string, number> {
|
|
const counts = new Map<string, number>();
|
|
for (const mention of pendingMentions) {
|
|
counts.set(mention.identityId, (counts.get(mention.identityId) ?? 0) + 1);
|
|
}
|
|
return counts;
|
|
}
|
|
|
|
export function selectReadyParticipants<TParticipant extends InitiativeParticipant>(
|
|
state: InitiativeState<TParticipant>,
|
|
maxJobs: number,
|
|
completedThisTick: Set<string>,
|
|
restStates: Map<string, RestSnapshot>,
|
|
idleCooling: IdleCoolingPolicy,
|
|
nowMs = Date.now(),
|
|
): TParticipant[] {
|
|
const mentionCounts = countPendingMentionsByIdentity(state.pendingMentions);
|
|
const ready = state.participants
|
|
.filter((participant) => participant.status === "active" && participant.currentLoad < 1)
|
|
.filter((participant) => !completedThisTick.has(participant.identityId))
|
|
.filter((participant) => participant.participantKind !== "repo_face" || (mentionCounts.get(participant.identityId) ?? 0) > 0 || restStates.get(participant.identityId)?.isNapping !== true)
|
|
.filter((participant) => participant.nextTurnAt <= state.initiativeClock)
|
|
.sort((left, right) => {
|
|
const mentionDelta = (mentionCounts.get(right.identityId) ?? 0) - (mentionCounts.get(left.identityId) ?? 0);
|
|
return mentionDelta || left.nextTurnAt - right.nextTurnAt || right.reactionBias - left.reactionBias || right.effectiveSpeed - left.effectiveSpeed || left.identityId.localeCompare(right.identityId);
|
|
});
|
|
|
|
if (!idleCooling.enabled || !idleCooling.active) return ready.slice(0, maxJobs);
|
|
const mentioned = ready.filter((participant) => (mentionCounts.get(participant.identityId) ?? 0) > 0);
|
|
const unprompted = ready.filter((participant) => (mentionCounts.get(participant.identityId) ?? 0) === 0);
|
|
const allowsUnprompted = !idleCooling.nextUnpromptedTurnAllowedAt || Date.parse(idleCooling.nextUnpromptedTurnAllowedAt) <= nowMs;
|
|
const cooled = allowsUnprompted && mentioned.length < maxJobs ? unprompted.slice(0, 1) : [];
|
|
return [...mentioned, ...cooled].slice(0, maxJobs);
|
|
}
|
|
|
|
function round3(value: number): number {
|
|
return Math.round(value * 1000) / 1000;
|
|
}
|
|
|
|
function initiativeSpeedFor(spec: ParticipantSpec, overrides: Record<string, number>): number {
|
|
const override = overrides[spec.id.toLowerCase()];
|
|
if (override !== undefined) return clamp(override, 0.35, 6);
|
|
if (spec.id === "void") return 1;
|
|
return clamp(0.85 + stableUnit(spec.id, "speed") * 0.45, 0.75, 1.3);
|
|
}
|
|
|
|
function recoveryFor(participant: InitiativeParticipant): number {
|
|
const loadPenalty = 1 + participant.currentLoad * 0.75;
|
|
return (participant.baseRecoveryMinutes * loadPenalty) / Math.max(participant.effectiveSpeed, 0.1);
|
|
}
|
|
|
|
function initiativeGroupsFor(spec: ParticipantSpec): string[] {
|
|
return Array.from(new Set([
|
|
"all",
|
|
`kind:${spec.participantKind}`,
|
|
`turn:${spec.turnKind}`,
|
|
`identity:${normalizeKey(spec.id)}`,
|
|
`repo:${normalizeKey(spec.repoName)}`,
|
|
`display:${normalizeKey(spec.displayName)}`,
|
|
...spec.allowedChannelIds.map((channelId) => `channel:${channelId}`),
|
|
]));
|
|
}
|
|
|
|
function heatFor(spec: ParticipantSpec, groups: string[], globalHeat: number, overrides: Record<string, number>): number {
|
|
const keys = ["all", ...groups, normalizeKey(spec.id), normalizeKey(spec.repoName), normalizeKey(spec.displayName)];
|
|
return clamp(keys.reduce((heat, key) => heat * (overrides[key] ?? 1), globalHeat), 0.05, 20);
|
|
}
|
|
|
|
function reactionBiasFor(spec: ParticipantSpec): number {
|
|
return spec.id === "void" ? 0.55 : clamp(0.2 + stableUnit(spec.id, "reaction") * 0.55, 0.2, 0.75);
|
|
}
|
|
|
|
function interruptThresholdFor(spec: ParticipantSpec): number {
|
|
return spec.id === "void" ? 0.5 : clamp(0.45 + stableUnit(spec.id, "threshold") * 0.35, 0.45, 0.8);
|
|
}
|
|
|
|
function stableUnit(id: string, salt: string): number {
|
|
const hex = createHash("sha1").update(`${id}:${salt}`).digest("hex").slice(0, 8);
|
|
return Number.parseInt(hex, 16) / 0xffffffff;
|
|
}
|
|
|
|
function mergeStrings(values: string[], value: string): string[] {
|
|
return Array.from(new Set([...values, value]));
|
|
}
|
|
|
|
function normalizeKey(value: string): string {
|
|
return value.trim().toLowerCase();
|
|
}
|
|
|
|
function clamp(value: number, min: number, max: number): number {
|
|
return Math.min(max, Math.max(min, Number(value.toFixed(3))));
|
|
}
|