Extract Persona Discord activity source

This commit is contained in:
Meta Vens 2026-07-15 23:02:01 +01:00
commit 9f48f6a9e3
7 changed files with 239 additions and 136 deletions

View file

@ -64,24 +64,14 @@ import {
type PersonaSchedulerState as FaceHeartbeatState,
} from "../apps/persona-scheduler/dist/state-store.js";
import { scanActivePersonaTurns } from "../apps/persona-scheduler/dist/active-turn-source.js";
import {
readDiscordActivitySnapshot,
type IdleCoolingSnapshot,
} from "../apps/persona-scheduler/dist/discord-activity-source.js";
const HEARTBEAT_COMMAND = "repo-face-rumination";
const MAX_FACE_IMAGE_ATTACHMENT_BYTES = 12 * 1024 * 1024;
interface IdleCoolingSnapshot {
enabled: boolean;
active: boolean;
reason?: string;
checkedChannelIds: string[];
lastHumanActivityAt?: string;
idleForMinutes?: number;
idleAfterMinutes: number;
recoveryMinutes: number;
lastUnpromptedTurnQueuedAt?: string;
nextUnpromptedTurnAllowedAt?: string;
observedHumanMessages: Array<SourceMessage & { channelId: string }>;
}
interface RepoFaceChannelPlan {
primaryChannelId?: string;
snapshotChannelIds: string[];
@ -238,10 +228,16 @@ async function main(): Promise<void> {
);
rescheduleStaleOverdueParticipants(state);
applyPendingMentionPriority(state);
const idleCooling = await readIdleCoolingSnapshot({
config,
identities: registry.identities,
state,
const idleCooling = await readDiscordActivitySnapshot({
botToken: config.botToken,
channelIds: [
config.repoFaceHeartbeats.defaultChannelId,
config.bifrostDiscordChannelId,
...config.indexedChannelIds,
...registry.identities.flatMap((identity) => getRepoDiscordIdentityAllowedChannelIds(identity)),
],
policy: config.repoFaceHeartbeats.idleCooling,
history: state.history,
now,
});
await applySemanticResponsePressure({
@ -1293,120 +1289,6 @@ function pendingMentionsForParticipant(
.sort((left, right) => Date.parse(left.queuedAt) - Date.parse(right.queuedAt));
}
async function readIdleCoolingSnapshot(input: {
config: ReturnType<typeof loadConfig>;
identities: RepoDiscordIdentity[];
state: FaceHeartbeatState;
now: Date;
}): Promise<IdleCoolingSnapshot> {
const policy = input.config.repoFaceHeartbeats.idleCooling;
const checkedChannelIds = collectIdleCoolingChannelIds(input.config, input.identities);
const base = {
enabled: policy.enabled,
active: false,
checkedChannelIds,
idleAfterMinutes: policy.idleAfterMinutes,
recoveryMinutes: policy.recoveryMinutes,
lastUnpromptedTurnQueuedAt: newestUnpromptedTurnQueuedAt(input.state),
observedHumanMessages: [] as Array<SourceMessage & { channelId: string }>,
};
if (!input.config.botToken) {
return { ...base, reason: "missing_discord_bot_token" };
}
if (checkedChannelIds.length === 0) {
return { ...base, reason: "no_watched_discord_channels" };
}
let newestHumanActivityAt: string | undefined;
const fetchErrors: string[] = [];
for (const channelId of checkedChannelIds) {
try {
const messages = await fetchRecentDiscordMessages({
botToken: input.config.botToken,
channelId,
limit: 10,
});
for (const message of messages) {
if (message.isBot || !message.content.trim()) {
continue;
}
if (!base.observedHumanMessages.some((entry) => entry.id === message.id)) {
base.observedHumanMessages.push({ ...message, channelId });
}
const timestampMs = Date.parse(message.timestamp);
const newestMs = Date.parse(newestHumanActivityAt ?? "");
if (Number.isFinite(timestampMs) && (!Number.isFinite(newestMs) || timestampMs > newestMs)) {
newestHumanActivityAt = message.timestamp;
}
}
} catch (error) {
fetchErrors.push(`${channelId}: ${error instanceof Error ? error.message : String(error)}`);
}
}
const lastUnpromptedTurnQueuedAt = base.lastUnpromptedTurnQueuedAt;
const lastUnpromptedTurnQueuedMs = Date.parse(lastUnpromptedTurnQueuedAt ?? "");
const nextUnpromptedTurnAllowedAt = Number.isFinite(lastUnpromptedTurnQueuedMs)
? new Date(lastUnpromptedTurnQueuedMs + policy.recoveryMinutes * 60_000).toISOString()
: undefined;
if (!policy.enabled) {
return {
...base,
reason: fetchErrors.length > 0 ? "disabled_with_activity_fetch_failure" : "disabled",
lastHumanActivityAt: newestHumanActivityAt,
};
}
if (!newestHumanActivityAt) {
return {
...base,
active: true,
reason: fetchErrors.length > 0 ? "activity_fetch_failed_or_no_human_messages" : "no_recent_human_messages",
nextUnpromptedTurnAllowedAt,
};
}
const idleForMinutes = Math.max(0, (input.now.getTime() - Date.parse(newestHumanActivityAt)) / 60_000);
return {
...base,
active: idleForMinutes >= policy.idleAfterMinutes,
reason: fetchErrors.length > 0 ? "partial_activity_fetch_failure" : undefined,
lastHumanActivityAt: newestHumanActivityAt,
idleForMinutes: round3(idleForMinutes),
nextUnpromptedTurnAllowedAt,
};
}
function collectIdleCoolingChannelIds(
config: ReturnType<typeof loadConfig>,
identities: RepoDiscordIdentity[],
): string[] {
return Array.from(new Set([
config.repoFaceHeartbeats.defaultChannelId,
config.bifrostDiscordChannelId,
...config.indexedChannelIds,
...identities.flatMap((identity) => getRepoDiscordIdentityAllowedChannelIds(identity)),
].filter((value): value is string => typeof value === "string" && value.trim().length > 0)));
}
function newestUnpromptedTurnQueuedAt(state: FaceHeartbeatState): string | undefined {
for (const entry of [...state.history].reverse()) {
if (entry.type !== "queued" && entry.type !== "dry_run_selected") {
continue;
}
if (typeof entry.queuedAt !== "string") {
continue;
}
if (typeof entry.pendingMentionCount === "number" && entry.pendingMentionCount > 0) {
continue;
}
return entry.queuedAt;
}
return undefined;
}
function buildHeartbeatPrompt(input: {
identity: RepoDiscordIdentity;
channelId: string;

View file

@ -28,6 +28,10 @@ import {
parsePersonaTurnIdentity,
staleActiveTurnThresholdMs,
} from "../apps/persona-scheduler/dist/active-turn-source.js";
import {
newestUnpromptedTurnQueuedAt,
readDiscordActivitySnapshot,
} from "../apps/persona-scheduler/dist/discord-activity-source.js";
function participant(identityId: string, nextTurnAt: number): InitiativeParticipant {
return {
@ -169,6 +173,37 @@ assert.equal(parsePersonaTurnIdentity("agent-turn:nibu:2026-07-15"), "nibu", "th
assert.equal(parsePersonaTurnIdentity("unrelated:job"), undefined, "unrelated jobs cannot freeze Persona initiative");
assert.equal(staleActiveTurnThresholdMs(20 * 60_000), 60 * 60_000, "runtime timeout expands the stale-claim boundary");
assert.equal(staleActiveTurnThresholdMs(5 * 60_000), 45 * 60_000, "stale recovery never becomes an impatient repair loop");
assert.equal(newestUnpromptedTurnQueuedAt([
{ type: "queued", queuedAt: "2026-07-15T19:00:00.000Z", pendingMentionCount: 0 },
{ type: "queued", queuedAt: "2026-07-15T20:00:00.000Z", pendingMentionCount: 1 },
]), "2026-07-15T19:00:00.000Z", "direct attention cannot consume the unprompted-turn cooling window");
const activitySnapshot = await readDiscordActivitySnapshot({
botToken: "test-token",
channelIds: ["aquarium", "aquarium", undefined],
policy: { enabled: true, idleAfterMinutes: 30, recoveryMinutes: 90 },
history: [{ type: "queued", queuedAt: "2026-07-15T19:30:00.000Z", pendingMentionCount: 0 }],
now: new Date("2026-07-15T21:00:00.000Z"),
fetchImpl: async () => new Response(JSON.stringify([
{
id: "bot-message",
author: { id: "bot", username: "VoidBot", bot: true },
content: "machine noise",
timestamp: "2026-07-15T20:55:00.000Z",
},
{
id: "human-message",
author: { id: "human", username: "Human", global_name: "Operator" },
content: "still here",
timestamp: "2026-07-15T20:45:00.000Z",
},
]), { status: 200, headers: { "content-type": "application/json" } }),
});
assert.deepEqual(activitySnapshot.checkedChannelIds, ["aquarium"], "activity acquisition deduplicates its explicit watched channels");
assert.deepEqual(activitySnapshot.observedHumanMessages.map((message) => message.id), ["human-message"], "bot output cannot impersonate human room activity");
assert.equal(activitySnapshot.active, false, "recent human activity keeps idle cooling inactive");
assert.equal(activitySnapshot.idleForMinutes, 15, "idle duration is a neutral wall-clock observation");
assert.equal(activitySnapshot.nextUnpromptedTurnAllowedAt, "2026-07-15T21:00:00.000Z", "the activity source derives the next unprompted window without mutating initiative");
const stateDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-scheduler-"));
try {