Move participant reconciliation into scheduler
This commit is contained in:
parent
560638efdd
commit
f85710f777
6 changed files with 237 additions and 175 deletions
|
|
@ -49,8 +49,10 @@ import {
|
|||
advanceInitiativeClockFromWallClock,
|
||||
applyPendingMentionPriority,
|
||||
countPendingMentionsByIdentity,
|
||||
reconcileParticipants,
|
||||
rescheduleStaleOverdueParticipants,
|
||||
selectReadyParticipants,
|
||||
type ParticipantSpec as SchedulerParticipantSpec,
|
||||
} from "../apps/persona-scheduler/dist/initiative-engine.js";
|
||||
|
||||
const HEARTBEAT_SCHEMA_VERSION = REPO_FACE_HEARTBEAT_SCHEMA_VERSION;
|
||||
|
|
@ -266,16 +268,17 @@ async function main(): Promise<void> {
|
|||
state.baseRecoveryMinutes = config.repoFaceHeartbeats.baseRecoveryMinutes;
|
||||
state.globalHeat = globalHeat;
|
||||
const completedThisTick = new Set<string>();
|
||||
state.participants = reconcileParticipants(
|
||||
state.participants,
|
||||
buildParticipantSpecs(registry.identities, config.voidModerationHeartbeatEnabled),
|
||||
config.repoFaceHeartbeats.defaultChannelId,
|
||||
config.repoFaceHeartbeats.speedOverrides,
|
||||
config.repoFaceHeartbeats.heatOverrides,
|
||||
state.initiativeClock,
|
||||
config.repoFaceHeartbeats.baseRecoveryMinutes,
|
||||
const participantSpecs = buildParticipantSpecs(registry.identities, config.voidModerationHeartbeatEnabled);
|
||||
state.participants = reconcileParticipants({
|
||||
existing: state.participants,
|
||||
specs: participantSpecs,
|
||||
defaultChannelId: config.repoFaceHeartbeats.defaultChannelId,
|
||||
speedOverrides: config.repoFaceHeartbeats.speedOverrides,
|
||||
heatOverrides: config.repoFaceHeartbeats.heatOverrides,
|
||||
initiativeClock: state.initiativeClock,
|
||||
baseRecoveryMinutes: config.repoFaceHeartbeats.baseRecoveryMinutes,
|
||||
globalHeat,
|
||||
).map((participant) =>
|
||||
}).map((participant) =>
|
||||
applyActiveTurnFreeze(
|
||||
participant,
|
||||
activeTurnScan.active.get(participant.identityId),
|
||||
|
|
@ -293,7 +296,7 @@ async function main(): Promise<void> {
|
|||
});
|
||||
await applySemanticResponsePressure({
|
||||
state,
|
||||
specs: buildParticipantSpecs(registry.identities, config.voidModerationHeartbeatEnabled),
|
||||
specs: participantSpecs,
|
||||
messages: idleCooling.observedHumanMessages,
|
||||
restStates,
|
||||
completedThisTick,
|
||||
|
|
@ -473,14 +476,7 @@ async function readAgentSwarmPause(): Promise<{ paused: boolean; path: string; r
|
|||
}
|
||||
}
|
||||
|
||||
interface ParticipantSpec {
|
||||
id: string;
|
||||
participantKind: FaceHeartbeatParticipant["participantKind"];
|
||||
turnKind: FaceHeartbeatParticipant["turnKind"];
|
||||
repoName: string;
|
||||
displayName: string;
|
||||
allowedChannelIds: string[];
|
||||
channelSpeedMultiplier: number;
|
||||
interface ParticipantSpec extends SchedulerParticipantSpec {
|
||||
identity?: RepoDiscordIdentity;
|
||||
}
|
||||
|
||||
|
|
@ -1311,92 +1307,6 @@ async function readRecentLock(path: string, maxAgeMinutes: number): Promise<bool
|
|||
}
|
||||
}
|
||||
|
||||
function reconcileParticipants(
|
||||
existing: FaceHeartbeatParticipant[],
|
||||
specs: ParticipantSpec[],
|
||||
defaultChannelId: string | undefined,
|
||||
speedOverrides: Record<string, number>,
|
||||
heatOverrides: Record<string, number>,
|
||||
initiativeClock: number,
|
||||
baseRecoveryMinutes: number,
|
||||
globalHeat: number,
|
||||
): FaceHeartbeatParticipant[] {
|
||||
const existingById = new Map(existing.map((entry) => [entry.identityId, entry]));
|
||||
const count = Math.max(specs.length, 1);
|
||||
|
||||
return specs.map((spec, index) => {
|
||||
const current = existingById.get(spec.id);
|
||||
const hasChannel = spec.participantKind === "system_agent" || Boolean(spec.allowedChannelIds[0] || defaultChannelId);
|
||||
const speed = initiativeSpeedFor(spec, speedOverrides) * spec.channelSpeedMultiplier;
|
||||
const groups = initiativeGroupsFor(spec);
|
||||
const heat = heatFor(spec, groups, globalHeat, 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
|
||||
: initiativeClock + ((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,
|
||||
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,
|
||||
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.",
|
||||
],
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function applyActiveTurnFreeze(
|
||||
participant: FaceHeartbeatParticipant,
|
||||
activeJobId: string | undefined,
|
||||
|
|
@ -5590,76 +5500,11 @@ function isRepoFacePendingMention(value: unknown): value is RepoFacePendingMenti
|
|||
);
|
||||
}
|
||||
|
||||
function initiativeSpeedFor(
|
||||
spec: ParticipantSpec,
|
||||
speedOverrides: Record<string, number>,
|
||||
): number {
|
||||
const override = speedOverrides[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 channelSpeedMultiplierFor(identity: RepoDiscordIdentity): number {
|
||||
const multipliers = identity.channelPermissions.map((permission) => permission.speedMultiplier);
|
||||
return multipliers.length > 0 ? clamp(Math.max(...multipliers), 0.5, 3) : 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,
|
||||
heatOverrides: Record<string, number>,
|
||||
): number {
|
||||
const keys = [
|
||||
"all",
|
||||
...groups,
|
||||
normalizeKey(spec.id),
|
||||
normalizeKey(spec.repoName),
|
||||
normalizeKey(spec.displayName),
|
||||
];
|
||||
return clamp(
|
||||
keys.reduce((heat, key) => heat * (heatOverrides[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]));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ import assert from "node:assert/strict";
|
|||
import {
|
||||
advanceInitiativeClockFromWallClock,
|
||||
applyPendingMentionPriority,
|
||||
reconcileParticipants,
|
||||
selectReadyParticipants,
|
||||
type InitiativeParticipant,
|
||||
type InitiativeState,
|
||||
|
|
@ -12,12 +13,25 @@ function participant(identityId: string, nextTurnAt: number): InitiativeParticip
|
|||
return {
|
||||
identityId,
|
||||
participantKind: "repo_face",
|
||||
turnKind: "repo_face_rumination",
|
||||
repoName: identityId,
|
||||
displayName: identityId,
|
||||
initiativeSpeed: 1,
|
||||
status: "active",
|
||||
currentLoad: 0,
|
||||
nextTurnAt,
|
||||
baseRecoveryMinutes: 30,
|
||||
reactionBias: 1,
|
||||
interruptThreshold: 0.6,
|
||||
groups: [],
|
||||
heat: 1,
|
||||
dynamicHeat: 1,
|
||||
responsePressure: 0,
|
||||
responsePressureEvidence: [],
|
||||
semanticInterruptReceipts: [],
|
||||
effectiveSpeed: 1,
|
||||
queuedCount: 0,
|
||||
constraints: [],
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -54,4 +68,40 @@ const cooled = selectReadyParticipants(
|
|||
);
|
||||
assert.deepEqual(cooled.map((entry) => entry.identityId), ["mentioned"], "idle cooling suppresses unprompted turns but never pending mentions");
|
||||
|
||||
const blocked = { ...participant("existing", 44), status: "blocked" as const, currentLoad: 1, activeJobId: "job-1" };
|
||||
const reconciled = reconcileParticipants({
|
||||
existing: [blocked],
|
||||
specs: [
|
||||
{
|
||||
id: "existing",
|
||||
participantKind: "native_persona",
|
||||
turnKind: "repo_face_rumination",
|
||||
repoName: "New body",
|
||||
displayName: "Existing",
|
||||
allowedChannelIds: ["room"],
|
||||
channelSpeedMultiplier: 2,
|
||||
},
|
||||
{
|
||||
id: "channel-less",
|
||||
participantKind: "repo_face",
|
||||
turnKind: "repo_face_rumination",
|
||||
repoName: "No mouth",
|
||||
displayName: "No mouth",
|
||||
allowedChannelIds: [],
|
||||
channelSpeedMultiplier: 1,
|
||||
},
|
||||
],
|
||||
speedOverrides: { existing: 1.5 },
|
||||
heatOverrides: { "identity:existing": 0.5 },
|
||||
initiativeClock: 40,
|
||||
baseRecoveryMinutes: 20,
|
||||
globalHeat: 1.2,
|
||||
});
|
||||
assert.equal(reconciled[0].status, "blocked", "reconciliation preserves an explicit manual block");
|
||||
assert.equal(reconciled[0].activeJobId, "job-1", "reconciliation preserves live load ownership");
|
||||
assert.equal(reconciled[0].nextTurnAt, 44, "reconciliation preserves initiative position");
|
||||
assert.equal(reconciled[0].initiativeSpeed, 3, "operator speed and channel multiplier are projected once");
|
||||
assert.equal(reconciled[0].heat, 0.6, "global and identity heat compose in the scheduler owner");
|
||||
assert.equal(reconciled[1].status, "blocked", "a new Face without any mouth fails closed");
|
||||
|
||||
process.stdout.write("Persona initiative engine smoke passed.\n");
|
||||
|
|
|
|||
Loading…
Reference in a new issue