diff --git a/apps/persona-scheduler/package.json b/apps/persona-scheduler/package.json index bbe8a30..acc675c 100644 --- a/apps/persona-scheduler/package.json +++ b/apps/persona-scheduler/package.json @@ -8,6 +8,7 @@ "@voidbot/config": "0.1.0", "@voidbot/core": "0.1.0", "@voidbot/rag": "0.1.0", + "@voidbot/shared": "0.1.0", "dotenv": "^16.4.0" } } diff --git a/apps/persona-scheduler/src/turn-context-source.ts b/apps/persona-scheduler/src/turn-context-source.ts new file mode 100644 index 0000000..b636793 --- /dev/null +++ b/apps/persona-scheduler/src/turn-context-source.ts @@ -0,0 +1,198 @@ +import { createHash } from "node:crypto"; +import { mkdir, stat, writeFile } from "node:fs/promises"; +import { extname, resolve } from "node:path"; + +import type { SourceMessage, SourceMessageAttachment } from "@voidbot/shared"; + +const MAX_FACE_IMAGE_ATTACHMENT_BYTES = 12 * 1024 * 1024; + +export interface ChannelSnapshot { + channelId: string; + messages: SourceMessage[]; +} + +interface DiscordApiMessage { + id: string; + content: string; + timestamp: string; + attachments?: DiscordApiAttachment[]; + author: { + id: string; + username: string; + global_name?: string | null; + bot?: boolean; + }; + member?: { nick?: string | null }; +} + +interface DiscordApiAttachment { + id?: string; + filename?: string; + content_type?: string; + size?: number; + url?: string; + proxy_url?: string; + width?: number | null; + height?: number | null; +} + +export async function fetchRecentDiscordMessages(input: { + botToken?: string; + channelId: string; + limit: number; + ignoreBotMessages?: boolean; + fetchImpl?: typeof fetch; + mediaCacheRoot?: string; +}): Promise { + if (!input.botToken) return []; + const fetchImpl = input.fetchImpl ?? fetch; + const url = new URL(`https://discord.com/api/v10/channels/${input.channelId}/messages`); + url.searchParams.set("limit", String(Math.max(1, Math.min(input.limit, 25)))); + const response = await fetchImpl(url, { headers: { Authorization: `Bot ${input.botToken}` } }); + if (!response.ok) { + throw new Error(`Discord recent message fetch failed with ${response.status}: ${await response.text()}`); + } + + const messages = await response.json() as DiscordApiMessage[]; + const sourceMessages = await Promise.all(messages + .filter((message) => !(input.ignoreBotMessages && message.author.bot === true)) + .map(async (message) => { + const attachments = await materializeDiscordAttachments({ + channelId: input.channelId, + message, + fetchImpl, + mediaCacheRoot: input.mediaCacheRoot, + }); + return { + id: message.id, + authorId: message.author.id, + authorName: message.author.global_name ?? message.member?.nick ?? message.author.username, + content: message.content, + timestamp: message.timestamp, + isBot: message.author.bot === true, + ...(attachments.length > 0 ? { attachments } : {}), + }; + })); + return sourceMessages.sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp)); +} + +export async function fetchChannelSnapshots(input: { + botToken?: string; + channelIds: string[]; + primaryChannelId: string; + limit: number; + bifrostDiscordChannelId?: string; + fetchImpl?: typeof fetch; + mediaCacheRoot?: string; + now?: Date; +}): Promise { + const snapshots: ChannelSnapshot[] = []; + for (const channelId of input.channelIds.filter((entry) => entry !== input.primaryChannelId).slice(0, 5)) { + try { + snapshots.push({ + channelId, + messages: await fetchRecentDiscordMessages({ + botToken: input.botToken, + channelId, + limit: input.limit, + ignoreBotMessages: channelId === input.bifrostDiscordChannelId, + fetchImpl: input.fetchImpl, + mediaCacheRoot: input.mediaCacheRoot, + }), + }); + } catch (error) { + snapshots.push({ + channelId, + messages: [{ + id: `snapshot-error:${channelId}`, + authorId: "voidbot", + authorName: "VoidBot", + content: `Could not read recent channel context: ${error instanceof Error ? error.message : String(error)}`, + timestamp: (input.now ?? new Date()).toISOString(), + isBot: true, + }], + }); + } + } + return snapshots; +} + +async function materializeDiscordAttachments(input: { + channelId: string; + message: DiscordApiMessage; + fetchImpl: typeof fetch; + mediaCacheRoot?: string; +}): Promise { + const materialized: SourceMessageAttachment[] = []; + for (const attachment of (input.message.attachments ?? []).slice(0, 4)) { + const kind = isDiscordImageAttachment(attachment) ? "image" : "other"; + const localPath = kind === "image" && isWithinFaceImageSizeLimit(attachment) + ? await cacheDiscordImageAttachment({ ...input, messageId: input.message.id, attachment }) + : undefined; + materialized.push({ + kind, + id: attachment.id, + filename: attachment.filename, + contentType: attachment.content_type, + url: attachment.url, + proxyUrl: attachment.proxy_url, + size: typeof attachment.size === "number" ? attachment.size : undefined, + width: typeof attachment.width === "number" ? attachment.width : undefined, + height: typeof attachment.height === "number" ? attachment.height : undefined, + localPath, + }); + } + return materialized; +} + +async function cacheDiscordImageAttachment(input: { + channelId: string; + messageId: string; + attachment: DiscordApiAttachment; + fetchImpl: typeof fetch; + mediaCacheRoot?: string; +}): Promise { + const sourceUrl = input.attachment.url ?? input.attachment.proxy_url; + if (!sourceUrl) return undefined; + const fileStem = [ + input.messageId, + input.attachment.id ?? createHash("sha256").update(sourceUrl).digest("hex").slice(0, 12), + ].join("-"); + const directory = resolve(input.mediaCacheRoot ?? resolve(".voidbot", "media", "discord-images"), input.channelId); + const localPath = resolve(directory, `${fileStem}${normalizedImageExtension(input.attachment)}`); + try { + await stat(localPath); + return localPath; + } catch { + // Cache miss. + } + try { + const response = await input.fetchImpl(sourceUrl); + if (!response.ok) return undefined; + await mkdir(directory, { recursive: true }); + await writeFile(localPath, Buffer.from(await response.arrayBuffer())); + return localPath; + } catch { + return undefined; + } +} + +function isWithinFaceImageSizeLimit(attachment: DiscordApiAttachment): boolean { + return typeof attachment.size !== "number" || attachment.size <= MAX_FACE_IMAGE_ATTACHMENT_BYTES; +} + +function isDiscordImageAttachment(attachment: DiscordApiAttachment): boolean { + const contentType = attachment.content_type?.toLowerCase() ?? ""; + return contentType.startsWith("image/") || /\.(png|jpe?g|gif|webp)$/i.test(attachment.filename ?? attachment.url ?? ""); +} + +function normalizedImageExtension(attachment: DiscordApiAttachment): string { + const fromName = extname(attachment.filename ?? "").toLowerCase(); + if (/^\.(png|jpe?g|gif|webp)$/.test(fromName)) return fromName; + switch (attachment.content_type?.toLowerCase()) { + case "image/jpeg": return ".jpg"; + case "image/gif": return ".gif"; + case "image/webp": return ".webp"; + default: return ".png"; + } +} diff --git a/apps/persona-scheduler/tsconfig.json b/apps/persona-scheduler/tsconfig.json index 3af1977..9c2179d 100644 --- a/apps/persona-scheduler/tsconfig.json +++ b/apps/persona-scheduler/tsconfig.json @@ -17,6 +17,9 @@ }, { "path": "../../packages/rag" + }, + { + "path": "../../packages/shared" } ] } diff --git a/notes/fresh-workspace-handoff.md b/notes/fresh-workspace-handoff.md index 166a90f..6633a89 100644 --- a/notes/fresh-workspace-handoff.md +++ b/notes/fresh-workspace-handoff.md @@ -145,7 +145,7 @@ Git history and smoke artifacts carry routine proof. `state/evidence.jsonl` shou - Manual newborn/strengthening passes should use `prompts/repo-face-personality-seed.prompt.md`. It makes the intended rite explicit: inspect repo docs/source/state/history, migrate useful legacy `memory.json` material into typed `.cc` memories, seed values/needs/agency/social state only when they will change behavior, and keep `.voidbot/voice` plus `.voidbot/state` as the repo-local home. - Agent turns use one CTB/Final-Fantasy-X-style initiative queue. `apps/persona-scheduler` owns the resident one-minute pulse on Yggdrasil. `initiative-engine.ts` owns participant reconciliation plus clock, active-turn freeze/recovery, queue-result commits, eligibility, and ready ordering; `state-store.ts` alone owns load/migration/atomic persistence of scheduler state. Discord mention ingress is no longer a second whole-state writer: the bot creates immutable per-message `voidbot.persona_attention_command` CultCache inbox documents, and the scheduler ingests/commits/acknowledges them with replay-safe ordering. `active-turn-source.ts` alone reads Persona job liveness from Postgres/the remaining Void lock and may fail a stale queue claim; it returns facts to the engine and cannot edit initiative. `discord-activity-source.ts` owns read-only human-activity polling and idle-window derivation; it excludes bot traffic and cannot write state, consume mentions, or select work. The configured scheduler JSON path is isolated legacy storage behind the state-store port, not a second owner or the desired final state shape; replace it with CultCache rather than teaching orchestration about another format. `scripts/run-repo-face-heartbeats.ts` may build neutral specs from registered identities and invoke queue actuators, but it no longer derives or persists speed, groups, heat, reaction bias, interrupt threshold, status, load, counters, recovery, initiative position, active-job liveness, or idle-activity observations. It remains the temporary orchestration shell for semantic pressure, context acquisition, and prompt assembly. Registered repo Faces and native Personas queue normal `repo-face-rumination` jobs through Postgres, and active jobs freeze initiative until the worker completes or fails them. Built-in Void is omitted when its distinct moderation heartbeat organ is disabled, so the hosted scheduler never launches the old PowerShell rumination path. The Windows task installer and wrapper are deleted. Heat knobs remain `REPO_FACE_HEARTBEAT_GLOBAL_HEAT` and `REPO_FACE_HEARTBEAT_HEAT_OVERRIDES`; idle cooling and pending-mention priority remain scheduler inputs. - Dynamic heat is live as derived semantic response pressure. `semantic-pressure-source.ts` batches canonical Face affinity cards and recent permitted-channel human-message embeddings through the existing RAG embedder, applies recency decay/noisy-OR aggregation, and returns bounded projections for at most three scheduler-eligible Faces. It does not write scheduler state. `initiative-engine.ts` alone commits inspectable evidence, `dynamicHeat`, one-time semantic interrupt receipts, and readiness changes. Direct mentions remain obligations, and model/Discord failures fail cool without bypassing rest, active jobs, or scheduler capacity. -- Repo Face heartbeat prompt assembly now has one model projection boundary before the child Face sees context: `repo-face-state-projector.prompt.md` turns typed Face state into character-facing memory/affect/social prose. Recent room messages, nearby channel snapshots, and pending mentions are now deterministic raw transcript sections, oldest-to-newest, with explicit recency/correction rules. The deleted conversation projector was turning evidence into mushy consensus; do not revive it. The Face model must read human corrections directly. A separate deterministic social-affordance surface connects active typed person-bonds/status reads to recent raw messages by that person, so relationship state becomes a live hook instead of private flavor text. Model ownership is split: Face turns default to full `gpt-5.4`; state projection and parent Interpreter passes use the cheaper Spark/Mini support chain. +- Repo Face heartbeat prompt assembly now has one model projection boundary before the child Face sees context: `repo-face-state-projector.prompt.md` turns typed Face state into character-facing memory/affect/social prose. `turn-context-source.ts` owns raw Discord acquisition and its bounded attachment cache; it returns current-room and nearby-channel messages oldest-to-newest plus explicit nearby-channel failure witnesses, without reading Persona state or composing prompts. Pending mentions remain scheduler-owned input. The deleted conversation projector was turning evidence into mushy consensus; do not revive it. The Face model must read human corrections directly. A separate deterministic social-affordance surface connects active typed person-bonds/status reads to recent raw messages by that person, so relationship state becomes a live hook instead of private flavor text. Model ownership is split: Face turns default to full `gpt-5.4`; state projection and parent Interpreter passes use the cheaper Spark/Mini support chain. - Repo Face prompts now include read-only recent home-repo activity before Discord transcript. `scripts/run-repo-face-heartbeats.ts` calls `scripts/export-recent-repo-activity.mjs --repos --state-path --read-only --hours 96 --max-commits 5`, so Faces see current body/jurisdiction motion before reacting to room pressure. This is the fix for Mimir missing live camera-runtime work, not a downstream compensator telling it to apologize harder. - Repo Face prompts now carry deterministic human pronoun guidance outside the model-projected state surface. The 2026-05-22 Nibu misgendering happened because Metacrat's interaction profile correctly resolved explicit `she/her`, but that fact was only inside the state packet handed to the projector, and the projector omitted it. `repo-face-turn.prompt.md` now renders `Known human pronoun guidance` near the transcript and again near the final writing instruction; the renderer also chooses evidence supporting the resolved pronoun set rather than the freshest contradictory ambient read. - Worker delivery must treat Interpreter channel names as Face-local labels, not raw Discord ids. `SAY channel: aquarium` or `channel: narrative` resolves through the registered identity's `channelPermissions` before allow-list checks and webhook delivery. Earlier live Aqua/Nibu turns parsed `say=1` but did not post because the worker literally checked whether the identity was allowed in channel id `aquarium`; the post path now resolves labels and has a live Epiphany webhook receipt after restart. diff --git a/notes/voidbot-current-system-map.md b/notes/voidbot-current-system-map.md index d7fb5ec..e83419d 100644 --- a/notes/voidbot-current-system-map.md +++ b/notes/voidbot-current-system-map.md @@ -182,7 +182,7 @@ VoidBot runs on Yggdrasil. Idunn owns deployment and consumes upstream repositor - Repo Face proposal/article PRs, PR comments, registered-identity Discord posts, and r/GameCultOrg Reddit thread posts now cross through parent-owned action blocks before Bifrost bridge commands. Bylined essays use the Interpreter-owned `ARTICLE` block: the Face writes naturally, the Interpreter extracts structured title/description/author/date/tags/path/body fields, and deterministic worker code renders/validates YAML frontmatter before calling `BIFROST_ROOT/tools/bifrost-bridge.mjs` for a draft PR and persona announcement. Subreddit threads use the Interpreter-owned `REDDIT THREAD` block: the Face writes a public title/body/share line, and deterministic worker code calls Bifrost `reddit-post` with Persona flair. The old `VOIDBOT_REPO_IDENTITY_ARTICLE` JSON sentinel is cut. Proposal PR and PR-comment sentinels still exist as compatibility rails. This is not merely helper placement: Bifrost's original governance/labor platform role makes it the owner of governed public crossings for GameCult work, including GitHub, r/GameCultOrg Reddit transport/thread access, Discord-native work/member/swarm interactions, dispatch receipts, CultNet/CultCache intake, and future collaboration interfaces. Heimdall is the OAuth/account/capability gate; Bifrost is the bridge/request/routing/receipt authority. VoidBot still owns room/thread cognition, moderation judgment, archive/source retrieval, and Face personality. - Repo Face turns now receive a Bifrost governance digest from `BIFROST_ROOT/tools/governance-threads.mjs digest --repo --agent ` alongside recent Discord context. Bifrost typed CultCache topics/comments are the canonical home for feature requests, Face opinions, missing questions, approvals, and dispatch receipts; Discord is a mirror/evidence source. When `BIFROST_DISCORD_CHANNEL_ID` is configured for `#bifrost`, repo Face Discord context ignores mirrored bot chatter there because the authoritative activity arrives through the Bifrost digest. Human `#bifrost` messages should only become governance comments after Bifrost/Heimdall can link the Discord id to a registered actor. Repo-local asks that would send a human or Codex to add/fix/name/scaffold/document/test/investigate/implement belong on Bifrost with enough room/repo context for a claimed Codex turn; a Face may still speak characterfully in Aquarium, but not as the only dispatch surface. Legacy `UPDATE REQUEST` packets are parsed only as compatibility input and reconciled into Bifrost topics instead of immediate transport enqueue. Local `#bifrost` is channel `1506760343836430376`; work proposals, governance mirrors, and Bifrost dispatch receipts should go there, while Aquarium remains the casual/social swarm room. - Repo character turns now have a state-projector plus turn-Interpreter shape. The state-in boundary is `prompts/repo-face-state-projector.prompt.md`: typed state and identity material are translated into lived character prose before the child sees it. The state projector must surface values, drives, wounds, fascinations, aesthetic taste, dignity, mood, needs, social reads, bonds, agency pressure, and the personality-specific meaning of sparse or dense social topology when the state contains those facts. The deterministic state packet feeding that projector is intentionally verbose and parent-facing: it exposes private notes, values, activation vectors, runtime rest/speaking pressure, affect needs, bonds, status reads, mood dimensions, durable memories, short-term residue, agency pressures, incubating thoughts, candidate speech pressure, recent speech residue, social graph facts, room weather, and semantic curiosity graph attractors so every included variable can appear directly or bend behavior. The curiosity graph is built by seeding history/source vector retrieval from current room context, nearby channels, typed Face state, and home-jurisdiction identity, then decoding retrieved chunks into weighted attractor clusters with prominence, saturation, novelty, density, jurisdiction fit, suggested motion, and evidence labels. The projector owns turning those neutral facts into lived curiosity, boredom, fatigue, territorial pull, underexplored intrigue, or a closing/pivot impulse; deterministic code must not decide the feeling. Deterministic code may also attach neutral graph facts such as mapped people and unmapped registered peers to the projector packet, but it must not decide whether that feels like anxiety, territorial scouting, or nothing. Deterministic code attaches recent read-only home-repo activity before chat, using the Face's own state path as the repo-activity cursor source, so current implementation motion is visible as body context. Current-room and nearby-channel logs no longer cross a model conversation projector; deterministic code attaches raw oldest-to-newest transcript evidence with explicit recency/correction rules so the Face can notice human reframes directly. Deterministic code also attaches a social-affordance surface that matches active typed person-bonds/status reads to recent raw messages from that person, so relationship state arrives as a live hook rather than private decorative lore. Deterministic code still owns full prompt assembly and inspection through `scripts/run-repo-face-heartbeats.ts --assemble-prompt `; that path composes stable doctrine, allowed rooms, research/archive capability guidance, Bifrost digest, projected memory/affect prose, home-repo activity, live social affordances, and raw conversation evidence into the exact child prompt. It must not invent personality, mood, affect, or social memory from typed state. Situational obligations such as direct-call priority or first-public-introduction duty are emitted only when the scheduler packet proves they apply, not as standing prompt law. The child character writes a natural in-character turn on full `gpt-5.4` and may describe desired speech, repo/article proposals, social reads, needs, mood shifts, or memory-worthy thoughts without emitting action DSL blocks. The cheaper Spark/Mini parent Interpreter decides route/retry/drop and translates warranted effects into structured `STATE NOTE` and `SAY` blocks for the worker while governance dispatch remains paused. `STATE NOTE` blocks become typed short-term memory, affect needs, social bonds, status reads, mood dimensions, or agency pressure. The worker still accepts legacy compact JSON sentinels and legacy `UPDATE REQUEST` blocks for compatibility, but the live prompt path keeps transport and state syntax out of the character's roleplay surface. -- `apps/persona-scheduler` is the resident daemon organ that owns the serialized Persona wall-clock pulse on Yggdrasil. It invokes the remaining turn orchestrator without overlap and remains alive between pulses. `apps/persona-scheduler/src/initiative-engine.ts` owns the pure scheduling state machine: participant reconciliation, deterministic base speed/bias/threshold derivation, operator heat/speed projection, semantic-pressure commits, wall-clock advancement, active-turn freeze/completion recovery, queue-success/failure/dry-run commits, stale rescheduling, pending-mention priority, rest/idle eligibility, and ready ordering. Explicit blocked/withdrawn status, active load, semantic receipts, and initiative position survive reconciliation; new Faces without a usable mouth fail closed. `apps/persona-scheduler/src/state-store.ts` owns scheduler persistence and legacy migration. It creates state only when the path is absent, rejects malformed/unsupported state rather than erasing it, and atomically promotes complete writes. `packages/core/src/repo-face-heartbeat-queue.ts` no longer rewrites scheduler state: the Discord bot publishes one immutable `voidbot.persona_attention_command` CultCache `.cc` inbox document per addressed message. The scheduler store ingests and deduplicates those commands, persists them as pending attention, then acknowledges inbox files only after the durable commit; a crash before acknowledgement replays safely. `apps/persona-scheduler/src/active-turn-source.ts` owns the Postgres/legacy-lock liveness adapter. It recognizes only Persona turn request IDs, applies the bounded stale threshold, marks dead queue claims failed, and returns active/stale witnesses; it never writes scheduler state. `apps/persona-scheduler/src/discord-activity-source.ts` owns neutral Discord activity acquisition: it deduplicates explicit watched channels, excludes bot traffic, reports raw human messages, and derives idle-cooling times from scheduler history without writing state or selecting work. `apps/persona-scheduler/src/semantic-pressure-source.ts` owns embedding-backed affinity observation and returns bounded projections for eligible Personas and permitted channels. It cannot mutate initiative; `initiative-engine.ts` alone applies pressure, dynamic heat, interrupt readiness, dedupe receipts, and transition history. The configured scheduler JSON path is isolated legacy storage behind the store port and should be replaced with a typed CultCache `.cc` backing without changing the engine, inbox, or orchestration shell. `scripts/run-repo-face-heartbeats.ts` projects the identity registry into neutral participant specs and affinity candidates, invokes external queue actuators, and still owns broader context acquisition plus prompt assembly pending further extraction. External adapters report facts; only the initiative engine mutates load, counters, recovery position, pressure, and queue-transition history. Repo Faces and native Personas queue ordinary `repo-face-rumination` jobs through the canonical job queue. Built-in Void participates only when its separate moderation heartbeat organ is enabled; the hosted scheduler does not launch Windows PowerShell. +- `apps/persona-scheduler` is the resident daemon organ that owns the serialized Persona wall-clock pulse on Yggdrasil. It invokes the remaining turn orchestrator without overlap and remains alive between pulses. `apps/persona-scheduler/src/initiative-engine.ts` owns the pure scheduling state machine: participant reconciliation, deterministic base speed/bias/threshold derivation, operator heat/speed projection, semantic-pressure commits, wall-clock advancement, active-turn freeze/completion recovery, queue-success/failure/dry-run commits, stale rescheduling, pending-mention priority, rest/idle eligibility, and ready ordering. Explicit blocked/withdrawn status, active load, semantic receipts, and initiative position survive reconciliation; new Faces without a usable mouth fail closed. `apps/persona-scheduler/src/state-store.ts` owns scheduler persistence and legacy migration. It creates state only when the path is absent, rejects malformed/unsupported state rather than erasing it, and atomically promotes complete writes. `packages/core/src/repo-face-heartbeat-queue.ts` no longer rewrites scheduler state: the Discord bot publishes one immutable `voidbot.persona_attention_command` CultCache `.cc` inbox document per addressed message. The scheduler store ingests and deduplicates those commands, persists them as pending attention, then acknowledges inbox files only after the durable commit; a crash before acknowledgement replays safely. `apps/persona-scheduler/src/active-turn-source.ts` owns the Postgres/legacy-lock liveness adapter. It recognizes only Persona turn request IDs, applies the bounded stale threshold, marks dead queue claims failed, and returns active/stale witnesses; it never writes scheduler state. `apps/persona-scheduler/src/discord-activity-source.ts` owns neutral Discord activity acquisition: it deduplicates explicit watched channels, excludes bot traffic, reports raw human messages, and derives idle-cooling times from scheduler history without writing state or selecting work. `apps/persona-scheduler/src/semantic-pressure-source.ts` owns embedding-backed affinity observation and returns bounded projections for eligible Personas and permitted channels. It cannot mutate initiative; `initiative-engine.ts` alone applies pressure, dynamic heat, interrupt readiness, dedupe receipts, and transition history. `apps/persona-scheduler/src/turn-context-source.ts` owns current-room and nearby-channel Discord evidence plus the bounded image-attachment cache. It returns chronological source messages and explicit snapshot failure witnesses; it has no access to Persona state, prompt composition, or the job queue. The configured scheduler JSON path is isolated legacy storage behind the store port and should be replaced with a typed CultCache `.cc` backing without changing the engine, inbox, or orchestration shell. `scripts/run-repo-face-heartbeats.ts` projects the identity registry into neutral participant specs and affinity candidates, invokes external queue actuators, and still owns Bifrost/repo/Persona-state context interpretation plus prompt assembly pending further extraction. External adapters report facts; only the initiative engine mutates load, counters, recovery position, pressure, and queue-transition history. Repo Faces and native Personas queue ordinary `repo-face-rumination` jobs through the canonical job queue. Built-in Void participates only when its separate moderation heartbeat organ is enabled; the hosted scheduler does not launch Windows PowerShell. - The same scheduler derives semantic `responsePressure` for scheduler-eligible Personas by embedding canonical Face identity-card projections and recent human messages from channels each Face may enter. Pressure decays by message age, aggregates with noisy-OR, awakens at most three eligible Faces, and exposes its message-scoped cosine evidence. It produces bounded `dynamicHeat` for the next scheduled recovery calculation; an unseen message crossing the Persona's interrupt threshold may pull readiness to the current initiative clock exactly once. It cannot create a job, bypass rest or active-turn brakes, impersonate a direct mention, or write inferred desire into Persona state. - The Windows `GameCult Local Orchestrator` and per-organ scheduled-task wrapper no longer own Persona pulses. `voidbot.service` deploys `persona-scheduler` beside bot, worker, Qdrant, and the CultMesh publisher. Turn frequency is controlled by `REPO_FACE_HEARTBEAT_BASE_RECOVERY_MINUTES`, `REPO_FACE_HEARTBEAT_GLOBAL_HEAT`, `REPO_FACE_HEARTBEAT_SPEED_OVERRIDES`, and `REPO_FACE_HEARTBEAT_HEAT_OVERRIDES`. Heat keys can target `all`, `kind:`, `turn:`, `identity:`, `repo:`, `display:`, `channel:`, or the bare identity/repo/display key. - The local ignored registry currently includes Nibu for `AetheriaLore`, with Face state in `E:/Projects/AetheriaLore/.voidbot/state/nibu.cc`, Discord role id `1505918221453103247`, and an Aquarium channel allow-list. Nibu's Face state now treats AetheriaLore-first map-building, abrasive character voice, consensus-backed change agency, her own `Aetheria/Lore/Nibu.md` character-note stewardship, Nibu-authored lore essays, and tangential setting fascination as separate active doctrines. That tangential fascination points at embodied ship minds, junkyard abandonment, murderous autonomy, salvage, and save-scumming survival. The repo Face heartbeat organ is now pulsed by `GameCult Local Orchestrator`; after restarting the stack onto the new worker code, forced heartbeat job `76020f1e-0c55-424b-9e35-89ba7f77fcc3` completed without base-bot auto-posting. diff --git a/package-lock.json b/package-lock.json index 4ddbdf5..aa2c5e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,6 +40,7 @@ "@voidbot/config": "0.1.0", "@voidbot/core": "0.1.0", "@voidbot/rag": "0.1.0", + "@voidbot/shared": "0.1.0", "dotenv": "^16.4.0" } }, diff --git a/scripts/run-repo-face-heartbeats.ts b/scripts/run-repo-face-heartbeats.ts index aaa0b16..4c52d86 100644 --- a/scripts/run-repo-face-heartbeats.ts +++ b/scripts/run-repo-face-heartbeats.ts @@ -1,11 +1,10 @@ import "dotenv/config"; -import { createHash } from "node:crypto"; import { spawn, spawnSync } from "node:child_process"; import { appendFile, mkdir, readFile, stat, writeFile } from "node:fs/promises"; import { createRequire } from "node:module"; import { homedir } from "node:os"; -import { dirname, extname, resolve } from "node:path"; +import { dirname, resolve } from "node:path"; import { loadConfig } from "@voidbot/config"; import { @@ -34,7 +33,6 @@ import { type RepoFaceConversationFocus, type RetrievalResult, type SourceMessage, - type SourceMessageAttachment, } from "@voidbot/shared"; import { advanceInitiativeClockFromWallClock, @@ -62,9 +60,13 @@ import { type IdleCoolingSnapshot, } from "../apps/persona-scheduler/dist/discord-activity-source.js"; import { readSemanticPressure } from "../apps/persona-scheduler/dist/semantic-pressure-source.js"; +import { + fetchChannelSnapshots, + fetchRecentDiscordMessages, + type ChannelSnapshot, +} from "../apps/persona-scheduler/dist/turn-context-source.js"; const HEARTBEAT_COMMAND = "repo-face-rumination"; -const MAX_FACE_IMAGE_ATTACHMENT_BYTES = 12 * 1024 * 1024; interface RepoFaceChannelPlan { primaryChannelId?: string; @@ -110,11 +112,6 @@ interface BifrostGovernanceComment { createdAt: string; } -interface ChannelSnapshot { - channelId: string; - messages: SourceMessage[]; -} - async function main(): Promise { const config = loadConfig(); const dryRun = process.argv.includes("--dry-run"); @@ -773,191 +770,6 @@ function sleepCyclesEqual(left: unknown, right: unknown): boolean { return JSON.stringify(left) === JSON.stringify(right); } -async function fetchRecentDiscordMessages(input: { - botToken?: string; - channelId: string; - limit: number; - ignoreBotMessages?: boolean; -}): Promise { - if (!input.botToken) { - return []; - } - - const url = new URL(`https://discord.com/api/v10/channels/${input.channelId}/messages`); - url.searchParams.set("limit", String(Math.max(1, Math.min(input.limit, 25)))); - const response = await fetch(url, { - headers: { - Authorization: `Bot ${input.botToken}`, - }, - }); - - if (!response.ok) { - throw new Error(`Discord recent message fetch failed with ${response.status}: ${await response.text()}`); - } - - const messages = await response.json() as DiscordApiMessage[]; - const sourceMessages = await Promise.all(messages - .filter((message) => !(input.ignoreBotMessages && message.author.bot === true)) - .map(async (message) => { - const attachments = await materializeDiscordAttachments({ - channelId: input.channelId, - message, - }); - return { - id: message.id, - authorId: message.author.id, - authorName: message.author.global_name ?? message.member?.nick ?? message.author.username, - content: message.content, - timestamp: message.timestamp, - isBot: message.author.bot === true, - ...(attachments.length > 0 ? { attachments } : {}), - }; - })); - return sourceMessages - .sort((left, right) => Date.parse(left.timestamp) - Date.parse(right.timestamp)); -} - -async function fetchChannelSnapshots(input: { - botToken?: string; - channelIds: string[]; - primaryChannelId: string; - limit: number; - bifrostDiscordChannelId?: string; -}): Promise { - const snapshots: ChannelSnapshot[] = []; - for (const channelId of input.channelIds.filter((entry) => entry !== input.primaryChannelId).slice(0, 5)) { - try { - snapshots.push({ - channelId, - messages: await fetchRecentDiscordMessages({ - botToken: input.botToken, - channelId, - limit: input.limit, - ignoreBotMessages: channelId === input.bifrostDiscordChannelId, - }), - }); - } catch (error) { - const message = error instanceof Error ? error.message : String(error); - snapshots.push({ - channelId, - messages: [{ - id: `snapshot-error:${channelId}`, - authorId: "voidbot", - authorName: "VoidBot", - content: `Could not read recent channel context: ${message}`, - timestamp: new Date().toISOString(), - isBot: true, - }], - }); - } - } - return snapshots; -} - -async function materializeDiscordAttachments(input: { - channelId: string; - message: DiscordApiMessage; -}): Promise { - const attachments = input.message.attachments ?? []; - if (attachments.length === 0) { - return []; - } - - const materialized: SourceMessageAttachment[] = []; - for (const attachment of attachments.slice(0, 4)) { - const kind = isDiscordImageAttachment(attachment) ? "image" : "other"; - let localPath: string | undefined; - if (kind === "image" && isWithinFaceImageSizeLimit(attachment)) { - localPath = await cacheDiscordImageAttachment({ - channelId: input.channelId, - messageId: input.message.id, - attachment, - }); - } - materialized.push({ - kind, - id: attachment.id, - filename: attachment.filename, - contentType: attachment.content_type, - url: attachment.url, - proxyUrl: attachment.proxy_url, - size: typeof attachment.size === "number" ? attachment.size : undefined, - width: typeof attachment.width === "number" ? attachment.width : undefined, - height: typeof attachment.height === "number" ? attachment.height : undefined, - localPath, - }); - } - return materialized; -} - -function isWithinFaceImageSizeLimit(attachment: DiscordApiAttachment): boolean { - return typeof attachment.size !== "number" || attachment.size <= MAX_FACE_IMAGE_ATTACHMENT_BYTES; -} - -function isDiscordImageAttachment(attachment: DiscordApiAttachment): boolean { - const contentType = attachment.content_type?.toLowerCase() ?? ""; - if (contentType.startsWith("image/")) { - return true; - } - return /\.(png|jpe?g|gif|webp)$/i.test(attachment.filename ?? attachment.url ?? ""); -} - -async function cacheDiscordImageAttachment(input: { - channelId: string; - messageId: string; - attachment: DiscordApiAttachment; -}): Promise { - const sourceUrl = input.attachment.url ?? input.attachment.proxy_url; - if (!sourceUrl) { - return undefined; - } - const safeExtension = normalizedImageExtension(input.attachment); - const fileStem = [ - input.messageId, - input.attachment.id ?? createHash("sha256").update(sourceUrl).digest("hex").slice(0, 12), - ].join("-"); - const directory = resolve(".voidbot", "media", "discord-images", input.channelId); - const localPath = resolve(directory, `${fileStem}${safeExtension}`); - try { - await stat(localPath); - return localPath; - } catch { - // Cache miss. Fall through to fetch. - } - - try { - const response = await fetch(sourceUrl); - if (!response.ok) { - return undefined; - } - const arrayBuffer = await response.arrayBuffer(); - await mkdir(directory, { recursive: true }); - await writeFile(localPath, Buffer.from(arrayBuffer)); - return localPath; - } catch { - return undefined; - } -} - -function normalizedImageExtension(attachment: DiscordApiAttachment): string { - const fromName = extname(attachment.filename ?? "").toLowerCase(); - if (/^\.(png|jpe?g|gif|webp)$/.test(fromName)) { - return fromName; - } - const contentType = attachment.content_type?.toLowerCase(); - switch (contentType) { - case "image/jpeg": - return ".jpg"; - case "image/gif": - return ".gif"; - case "image/webp": - return ".webp"; - case "image/png": - default: - return ".png"; - } -} - async function fetchBifrostGovernanceDigest(input: { bifrostRoot: string; repoName: string; @@ -1020,33 +832,6 @@ async function fetchBifrostGovernanceDigest(input: { } } -interface DiscordApiMessage { - id: string; - content: string; - timestamp: string; - attachments?: DiscordApiAttachment[]; - author: { - id: string; - username: string; - global_name?: string | null; - bot?: boolean; - }; - member?: { - nick?: string | null; - }; -} - -interface DiscordApiAttachment { - id?: string; - filename?: string; - content_type?: string; - size?: number; - url?: string; - proxy_url?: string; - width?: number | null; - height?: number | null; -} - async function startVoidModerationTurn(input: { queuedAt: string; storageRoot: string; diff --git a/scripts/smoke-persona-initiative-engine.ts b/scripts/smoke-persona-initiative-engine.ts index 87d1276..2884a06 100644 --- a/scripts/smoke-persona-initiative-engine.ts +++ b/scripts/smoke-persona-initiative-engine.ts @@ -33,6 +33,10 @@ import { newestUnpromptedTurnQueuedAt, readDiscordActivitySnapshot, } from "../apps/persona-scheduler/dist/discord-activity-source.js"; +import { + fetchChannelSnapshots, + fetchRecentDiscordMessages, +} from "../apps/persona-scheduler/dist/turn-context-source.js"; function participant(identityId: string, nextTurnAt: number): InitiativeParticipant { return { @@ -233,6 +237,47 @@ assert.deepEqual(pressureState.participants[0].semanticInterruptReceipts, ["pres assert.equal(pressureState.participants[1].responsePressure, 0, "missing projections explicitly cool stale pressure"); assert.equal(pressureState.history.at(-1)?.type, "semantic_response_interrupt", "the state owner records the semantic transition"); +const contextMessages = await fetchRecentDiscordMessages({ + botToken: "test-token", + channelId: "aquarium", + limit: 15, + ignoreBotMessages: true, + fetchImpl: async () => new Response(JSON.stringify([ + { + id: "newer", + author: { id: "human-2", username: "Second" }, + content: "second", + timestamp: "2026-07-15T21:00:00.000Z", + attachments: [{ id: "text", filename: "note.txt", url: "https://example.invalid/note.txt" }], + }, + { + id: "ignored-bot", + author: { id: "bot", username: "Mirror", bot: true }, + content: "mirror", + timestamp: "2026-07-15T20:59:00.000Z", + }, + { + id: "older", + author: { id: "human-1", username: "First", global_name: "First Human" }, + content: "first", + timestamp: "2026-07-15T20:58:00.000Z", + }, + ]), { status: 200, headers: { "content-type": "application/json" } }), +}); +assert.deepEqual(contextMessages.map((message) => message.id), ["older", "newer"], "turn context is raw chronological Discord evidence with configured bot mirrors removed"); +assert.equal(contextMessages[1].attachments?.[0].kind, "other", "non-image attachments remain inspectable without becoming media-cache writes"); + +const failedSnapshot = await fetchChannelSnapshots({ + botToken: "test-token", + channelIds: ["primary", "nearby"], + primaryChannelId: "primary", + limit: 6, + now: new Date("2026-07-15T21:00:00.000Z"), + fetchImpl: async () => new Response("nope", { status: 503 }), +}); +assert.equal(failedSnapshot[0].messages[0].id, "snapshot-error:nearby", "nearby-channel failure is returned as explicit evidence rather than aborting the turn"); +assert.equal(failedSnapshot[0].messages[0].timestamp, "2026-07-15T21:00:00.000Z", "context-source failure witnesses use the caller's observation clock"); + const stateDirectory = await mkdtemp(join(tmpdir(), "voidbot-persona-scheduler-")); try { const statePath = join(stateDirectory, "scheduler.json"); diff --git a/state/evidence.jsonl b/state/evidence.jsonl index c8a8c4d..d467792 100644 --- a/state/evidence.jsonl +++ b/state/evidence.jsonl @@ -395,3 +395,4 @@ {"ts":"2026-07-15T21:25:12Z","type":"verification","status":"ok","note":"Yggdrasil's hosted Bifrost provider store processed two scheduler-originated VoidBot Persona discord.post commands after Bifrost 03c6f35 taught the consumer to load packaged CultLib entrypoints; canonical receipts recorded Odin message 1527062285414305925 and metame message 1527062288882729022."} {"ts":"2026-07-15T22:01:49Z","type":"implementation","status":"ok","note":"Persona scheduler now owns neutral Discord activity and idle-cooling acquisition through discord-activity-source.ts; the source excludes bot traffic, derives timing without state mutation, and the giant heartbeat script no longer polls idle activity."} {"ts":"2026-07-15T22:05:02Z","type":"implementation","status":"ok","note":"Semantic response-pressure sensing and initiative mutation are now separate authorities: semantic-pressure-source.ts returns embedding-backed projections, while initiative-engine.ts alone commits heat, readiness, receipts, and history."} +{"ts":"2026-07-15T22:13:47Z","type":"implementation","status":"ok","note":"Raw Persona turn Discord acquisition now lives in turn-context-source.ts with chronological source messages, bounded attachment caching, and explicit nearby-channel failure witnesses; the heartbeat shell no longer owns Discord API or attachment-cache implementation."} diff --git a/state/map.yaml b/state/map.yaml index daf9476..e44a610 100644 --- a/state/map.yaml +++ b/state/map.yaml @@ -65,7 +65,7 @@ active_subgoals: - Treat `notes/agent-swarm-teardown-plan-2026-05-21.md` as the historical teardown authority and the restart checklist. The repo Face swarm is live again, but only because the scheduler, prompt, model-output logs, parent Interpreter, Bifrost routing, and worker side-effect rails now have explicit observable boundaries. Keep watching for prompt/parser/transport Jenga before adding new swarm features. - Make the new project-state surfaces the default continuity path for nontrivial VoidBot work. - Keep the rebuilt moderation/self-state foundation on canonical CultCache `.cc` typed operations, never an editable JSON working projection. - - The resident `apps/persona-scheduler` process inside `voidbot.service` owns repo Face and native Persona turn pulses on Yggdrasil. Its `initiative-engine.ts` owns participant reconciliation, operator heat/speed projection, semantic-pressure commits, wall-clock advancement, active-turn freeze/completion recovery, queue-result commits, stale rescheduling, mention priority, rest/idle eligibility, and ready-order selection as a pure state machine. `state-store.ts` is the sole persistence/migration port: missing state is explicit, legacy state migrates with a witness, malformed state fails closed, and same-directory temporary writes promote atomically. Discord attention crosses through immutable per-mention `voidbot.persona_attention_command` CultCache inbox documents; the bot creates commands, while the scheduler alone ingests, deduplicates, commits, and acknowledges them after durable state. The bot can no longer read-modify-write the whole heartbeat state. `active-turn-source.ts` is the narrow Postgres/legacy-lock adapter: it reports active job witnesses and marks truly stale queue claims failed, but it cannot mutate initiative. `discord-activity-source.ts` owns read-only Discord human-activity observation and idle-window derivation from explicit watched channels plus scheduler history; it cannot mutate initiative, consume attention, or select work. `semantic-pressure-source.ts` embeds eligible Persona affinity cards against recent permitted human messages and returns bounded pressure projections; only `initiative-engine.ts` may turn those facts into dynamic heat, readiness, interrupt receipts, or history. The current scheduler JSON file is tolerated legacy persistence behind the state-store port, not the target typed state shape; move that port to CultCache without reopening orchestration. The heartbeat orchestration script may project registered identities into neutral participant specs and affinity candidates and report external actuator results, but it cannot decide, persist, migrate, inspect queue liveness, poll idle activity, commit semantic pressure, or overwrite initiative fields. The Windows `GameCult Local Orchestrator` no longer owns Persona scheduling, and its repo-Face task installer/wrapper have been deleted. Void moderation, mood, watchdog, and Bifrost dispatch remain separate organs pending their own daemon migration; they must not be smuggled back into the Persona scheduler. + - The resident `apps/persona-scheduler` process inside `voidbot.service` owns repo Face and native Persona turn pulses on Yggdrasil. Its `initiative-engine.ts` owns participant reconciliation, operator heat/speed projection, semantic-pressure commits, wall-clock advancement, active-turn freeze/completion recovery, queue-result commits, stale rescheduling, mention priority, rest/idle eligibility, and ready-order selection as a pure state machine. `state-store.ts` is the sole persistence/migration port: missing state is explicit, legacy state migrates with a witness, malformed state fails closed, and same-directory temporary writes promote atomically. Discord attention crosses through immutable per-mention `voidbot.persona_attention_command` CultCache inbox documents; the bot creates commands, while the scheduler alone ingests, deduplicates, commits, and acknowledges them after durable state. The bot can no longer read-modify-write the whole heartbeat state. `active-turn-source.ts` is the narrow Postgres/legacy-lock adapter: it reports active job witnesses and marks truly stale queue claims failed, but it cannot mutate initiative. `discord-activity-source.ts` owns read-only Discord human-activity observation and idle-window derivation from explicit watched channels plus scheduler history; it cannot mutate initiative, consume attention, or select work. `semantic-pressure-source.ts` embeds eligible Persona affinity cards against recent permitted human messages and returns bounded pressure projections; only `initiative-engine.ts` may turn those facts into dynamic heat, readiness, interrupt receipts, or history. `turn-context-source.ts` owns raw current-room and nearby-channel Discord acquisition plus its bounded derived attachment cache; it returns chronological evidence and explicit snapshot failures without reading Persona state, composing prompts, or queuing work. The current scheduler JSON file is tolerated legacy persistence behind the state-store port, not the target typed state shape; move that port to CultCache without reopening orchestration. The heartbeat orchestration script may project registered identities into neutral participant specs and affinity candidates and report external actuator results, but it cannot decide, persist, migrate, inspect queue liveness, poll Discord, commit semantic pressure, or overwrite initiative fields. The Windows `GameCult Local Orchestrator` no longer owns Persona scheduling, and its repo-Face task installer/wrapper have been deleted. Void moderation, mood, watchdog, and Bifrost dispatch remain separate organs pending their own daemon migration; they must not be smuggled back into the Persona scheduler. - Keep legacy moderation mirrors, whole-state projection commits, and deleted memory-organ compensators from re-entering the active path before adding more moderation, mood, agency, or public-lane behavior. - Keep public-lane limits, moderation, and provider behavior constrained enough that one 4060 Ti box does not get flayed for sport. - Preserve boring observability and startup discipline around Postgres, Qdrant, Ollama, Docker, backups, scheduled tasks, watchdogs, and any private local fire-signal extensions.