Move moderation state behind CultCacheTS
This commit is contained in:
parent
682dbc5bce
commit
3e826a38c7
17 changed files with 622 additions and 67 deletions
123
scripts/moderation-state-store.mjs
Normal file
123
scripts/moderation-state-store.mjs
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
import { existsSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createRequire } from "node:module";
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const require = createRequire(import.meta.url);
|
||||
const coreDistPath = resolve(repoRoot, "packages/core/dist/moderation-state-store.js");
|
||||
|
||||
if (!existsSync(coreDistPath)) {
|
||||
throw new Error(`Missing built moderation-state-store helper at ${coreDistPath}. Run npm run build first.`);
|
||||
}
|
||||
|
||||
const {
|
||||
commitModerationStateWorkingView,
|
||||
ensureModerationStateStore,
|
||||
getModerationStateLegacyJsonPath,
|
||||
getModerationStateWorkingPath,
|
||||
readModerationStateCursor,
|
||||
setModerationStateCursor,
|
||||
} = require(coreDistPath);
|
||||
|
||||
async function main() {
|
||||
const [command, ...rest] = process.argv.slice(2);
|
||||
const args = parseArgs(rest);
|
||||
|
||||
if (!command) {
|
||||
throw new Error("Expected a moderation-state-store command.");
|
||||
}
|
||||
|
||||
const canonicalPath = requirePathArg(args, "canonical");
|
||||
const workingPath = args.working ?? getModerationStateWorkingPath(canonicalPath);
|
||||
const legacyJsonPath = args.legacy ?? getModerationStateLegacyJsonPath(canonicalPath);
|
||||
const templatePath = args.template;
|
||||
const paths = {
|
||||
canonicalPath,
|
||||
workingPath,
|
||||
legacyJsonPath,
|
||||
templatePath,
|
||||
};
|
||||
|
||||
switch (command) {
|
||||
case "ensure": {
|
||||
const result = await ensureModerationStateStore(paths);
|
||||
writeJson({
|
||||
canonicalPath: result.canonicalPath,
|
||||
workingPath: result.workingPath,
|
||||
createdCanonical: result.createdCanonical,
|
||||
migratedFromLegacyJson: result.migratedFromLegacyJson,
|
||||
cursor: result.state.moderation_runtime?.cursor ?? null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
case "read-cursor": {
|
||||
const cursor = await readModerationStateCursor(canonicalPath);
|
||||
writeJson(cursor);
|
||||
return;
|
||||
}
|
||||
case "commit-working-view": {
|
||||
const state = await commitModerationStateWorkingView(paths);
|
||||
writeJson({
|
||||
canonicalPath,
|
||||
workingPath,
|
||||
cursor: state.moderation_runtime?.cursor ?? null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
case "set-cursor": {
|
||||
const state = await setModerationStateCursor(paths, {
|
||||
lastReviewedMessageId: args["message-id"] ?? null,
|
||||
lastReviewedTimestamp: args.timestamp ?? null,
|
||||
});
|
||||
writeJson({
|
||||
canonicalPath,
|
||||
workingPath,
|
||||
cursor: state.moderation_runtime?.cursor ?? null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
default:
|
||||
throw new Error(`Unknown moderation-state-store command "${command}".`);
|
||||
}
|
||||
}
|
||||
|
||||
function parseArgs(argv) {
|
||||
const args = {};
|
||||
|
||||
for (let index = 0; index < argv.length; index += 1) {
|
||||
const token = argv[index];
|
||||
|
||||
if (!token.startsWith("--")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const key = token.slice(2);
|
||||
const next = argv[index + 1];
|
||||
|
||||
if (!next || next.startsWith("--")) {
|
||||
args[key] = "true";
|
||||
continue;
|
||||
}
|
||||
|
||||
args[key] = next;
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
function requirePathArg(args, key) {
|
||||
const value = args[key];
|
||||
if (typeof value !== "string" || value.trim().length === 0) {
|
||||
throw new Error(`Missing required --${key} path.`);
|
||||
}
|
||||
|
||||
return resolve(value);
|
||||
}
|
||||
|
||||
function writeJson(value) {
|
||||
process.stdout.write(`${JSON.stringify(value)}\n`);
|
||||
}
|
||||
|
||||
await main();
|
||||
|
|
@ -10,7 +10,8 @@ $repoRoot = Split-Path -Parent $PSScriptRoot
|
|||
Set-Location -LiteralPath $repoRoot
|
||||
$envFile = Join-Path $repoRoot ".env"
|
||||
$stateTemplatePath = Join-Path $repoRoot "config\moderation-agent-state-template.json"
|
||||
$stateFilePath = Join-Path $repoRoot ".voidbot\private\moderation-agent-state.json"
|
||||
$stateFilePath = Join-Path $repoRoot ".voidbot\private\moderation-agent-state.msgpack"
|
||||
$stateWorkingPath = Join-Path $repoRoot ".voidbot\private\moderation-agent-state.json"
|
||||
$statusDir = Join-Path $repoRoot ".voidbot\status"
|
||||
$logDir = Join-Path $repoRoot ".voidbot\logs"
|
||||
$statusPath = Join-Path $statusDir "moderation-rumination.json"
|
||||
|
|
@ -20,6 +21,7 @@ $lastMessagePath = Join-Path $statusDir "moderation-rumination-last-message.txt"
|
|||
$lockPath = Join-Path $statusDir "moderation-rumination.lock"
|
||||
$mcpServerPath = Join-Path $repoRoot "apps\worker\dist\mcp-server.js"
|
||||
$recentHistoryScriptPath = Join-Path $repoRoot "scripts\export-recent-discord-history.mjs"
|
||||
$stateStoreScriptPath = Join-Path $repoRoot "scripts\moderation-state-store.mjs"
|
||||
|
||||
function Read-DotEnv {
|
||||
param(
|
||||
|
|
@ -85,19 +87,6 @@ function Append-RunLog {
|
|||
Add-Content -Path $summaryLogPath -Value $timestamped -Encoding UTF8
|
||||
}
|
||||
|
||||
function Ensure-ModerationState {
|
||||
if (Test-Path $stateFilePath) {
|
||||
return
|
||||
}
|
||||
|
||||
if (-not (Test-Path $stateTemplatePath)) {
|
||||
throw "Missing moderation state template at $stateTemplatePath"
|
||||
}
|
||||
|
||||
New-Item -ItemType Directory -Force -Path (Split-Path -Parent $stateFilePath) | Out-Null
|
||||
Copy-Item -Path $stateTemplatePath -Destination $stateFilePath -Force
|
||||
}
|
||||
|
||||
function Read-JsonFile {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
|
|
@ -117,27 +106,6 @@ function Read-JsonFile {
|
|||
return $raw | ConvertFrom-Json
|
||||
}
|
||||
|
||||
function Set-ModerationCursor {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string] $Path,
|
||||
[AllowNull()] $Cursor
|
||||
)
|
||||
|
||||
$state = Read-JsonFile -Path $Path
|
||||
|
||||
if ($null -eq $state) {
|
||||
return
|
||||
}
|
||||
|
||||
if ($null -eq $state.moderation_runtime) {
|
||||
$state | Add-Member -NotePropertyName moderation_runtime -NotePropertyValue ([pscustomobject]@{}) -Force
|
||||
}
|
||||
|
||||
$state.moderation_runtime.cursor = $Cursor
|
||||
Write-JsonFile -Path $Path -Data $state
|
||||
}
|
||||
|
||||
function Split-CommandArgs {
|
||||
param(
|
||||
[string] $Value
|
||||
|
|
@ -215,6 +183,25 @@ function Invoke-CodexExec {
|
|||
}
|
||||
}
|
||||
|
||||
function Invoke-NodeJson {
|
||||
param(
|
||||
[Parameter(Mandatory = $true)]
|
||||
[string[]] $Arguments
|
||||
)
|
||||
|
||||
$output = & node @Arguments
|
||||
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "Node helper failed: node $($Arguments -join ' ')"
|
||||
}
|
||||
|
||||
if ([string]::IsNullOrWhiteSpace($output)) {
|
||||
return $null
|
||||
}
|
||||
|
||||
return $output | ConvertFrom-Json
|
||||
}
|
||||
|
||||
if (Test-Path $lockPath) {
|
||||
$lock = Get-Content -Path $lockPath -Raw | ConvertFrom-Json
|
||||
$startedAt = $null
|
||||
|
|
@ -264,7 +251,6 @@ $codexExecArgs = if ($envValues.ContainsKey("CODEX_EXEC_ARGS")) {
|
|||
@()
|
||||
}
|
||||
|
||||
Ensure-ModerationState
|
||||
New-Item -ItemType Directory -Force -Path $statusDir, $logDir | Out-Null
|
||||
|
||||
if (-not (Test-Path $mcpServerPath)) {
|
||||
|
|
@ -272,7 +258,8 @@ if (-not (Test-Path $mcpServerPath)) {
|
|||
}
|
||||
|
||||
$scriptPreflight = @(
|
||||
$recentHistoryScriptPath
|
||||
$recentHistoryScriptPath,
|
||||
$stateStoreScriptPath
|
||||
)
|
||||
|
||||
foreach ($requiredPath in $scriptPreflight) {
|
||||
|
|
@ -281,12 +268,15 @@ foreach ($requiredPath in $scriptPreflight) {
|
|||
}
|
||||
}
|
||||
|
||||
$preRunState = Read-JsonFile -Path $stateFilePath
|
||||
$priorCursor = if ($null -ne $preRunState -and $null -ne $preRunState.moderation_runtime) {
|
||||
$preRunState.moderation_runtime.cursor
|
||||
} else {
|
||||
$null
|
||||
}
|
||||
$stateEnsureResult = Invoke-NodeJson -Arguments @(
|
||||
$stateStoreScriptPath,
|
||||
"ensure",
|
||||
"--canonical", $stateFilePath,
|
||||
"--working", $stateWorkingPath,
|
||||
"--legacy", $stateWorkingPath,
|
||||
"--template", $stateTemplatePath
|
||||
)
|
||||
$priorCursor = $stateEnsureResult.cursor
|
||||
|
||||
$historyArgs = @($recentHistoryScriptPath)
|
||||
if ($null -ne $priorCursor -and -not [string]::IsNullOrWhiteSpace($priorCursor.lastReviewedTimestamp)) {
|
||||
|
|
@ -418,7 +408,31 @@ $finishedAtUtc = [DateTime]::UtcNow
|
|||
$stateUpdated = $false
|
||||
|
||||
if ($exitCode -eq 0) {
|
||||
Set-ModerationCursor -Path $stateFilePath -Cursor $observedCursor
|
||||
[void](Invoke-NodeJson -Arguments @(
|
||||
$stateStoreScriptPath,
|
||||
"commit-working-view",
|
||||
"--canonical", $stateFilePath,
|
||||
"--working", $stateWorkingPath
|
||||
))
|
||||
|
||||
if ($null -ne $observedCursor) {
|
||||
$cursorArgs = @(
|
||||
$stateStoreScriptPath,
|
||||
"set-cursor",
|
||||
"--canonical", $stateFilePath,
|
||||
"--working", $stateWorkingPath
|
||||
)
|
||||
|
||||
if (-not [string]::IsNullOrWhiteSpace([string]$observedCursor.lastReviewedMessageId)) {
|
||||
$cursorArgs += @("--message-id", [string]$observedCursor.lastReviewedMessageId)
|
||||
}
|
||||
|
||||
if (-not [string]::IsNullOrWhiteSpace([string]$observedCursor.lastReviewedTimestamp)) {
|
||||
$cursorArgs += @("--timestamp", [string]$observedCursor.lastReviewedTimestamp)
|
||||
}
|
||||
|
||||
[void](Invoke-NodeJson -Arguments $cursorArgs)
|
||||
}
|
||||
}
|
||||
|
||||
$stateWriteAfter = (Get-Item $stateFilePath).LastWriteTimeUtc
|
||||
|
|
@ -434,6 +448,7 @@ Write-JsonFile -Path $statusPath -Data @{
|
|||
failureMessage = $failureMessage
|
||||
noPost = [bool]$NoPost
|
||||
stateFile = $stateFilePath
|
||||
stateWorkingFile = $stateWorkingPath
|
||||
stateWriteBeforeUtc = $stateWriteBefore.ToString("o")
|
||||
stateWriteAfterUtc = $stateWriteAfter.ToString("o")
|
||||
stateUpdated = $stateUpdated
|
||||
|
|
|
|||
|
|
@ -1,11 +1,21 @@
|
|||
import { mkdirSync, openSync, closeSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { createRequire } from "node:module";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import { reconcileSemanticMemoryState } from "./void-memory-organ.mjs";
|
||||
|
||||
const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const statePath = resolve(repoRoot, ".voidbot/private/moderation-agent-state.json");
|
||||
const require = createRequire(import.meta.url);
|
||||
const {
|
||||
ensureModerationStateStore,
|
||||
getModerationStateWorkingPath,
|
||||
materializeModerationStateWorkingView,
|
||||
saveModerationState,
|
||||
} = require(resolve(repoRoot, "packages/core/dist/moderation-state-store.js"));
|
||||
const statePath = resolve(repoRoot, ".voidbot/private/moderation-agent-state.msgpack");
|
||||
const stateWorkingPath = getModerationStateWorkingPath(statePath);
|
||||
const stateTemplatePath = resolve(repoRoot, "config/moderation-agent-state-template.json");
|
||||
const statusPath = resolve(repoRoot, ".voidbot/status/void-mood-drift.json");
|
||||
const lockPath = resolve(repoRoot, ".voidbot/status/void-mood-drift.lock");
|
||||
const lastSpeechPath = resolve(repoRoot, ".voidbot/status/void-last-speech.json");
|
||||
|
|
@ -23,7 +33,12 @@ async function main() {
|
|||
return;
|
||||
}
|
||||
|
||||
const state = readJson(statePath);
|
||||
const { state } = await ensureModerationStateStore({
|
||||
canonicalPath: statePath,
|
||||
workingPath: stateWorkingPath,
|
||||
legacyJsonPath: stateWorkingPath,
|
||||
templatePath: stateTemplatePath,
|
||||
});
|
||||
const canonicalState = ensureObject(state.canonical_state);
|
||||
const runtime = ensureObject(state.moderation_runtime);
|
||||
const sleepCycle = updateSleepCycle(runtime, now);
|
||||
|
|
@ -167,7 +182,11 @@ async function main() {
|
|||
);
|
||||
|
||||
state.moderation_runtime = runtime;
|
||||
writeJson(statePath, state);
|
||||
await saveModerationState(statePath, state);
|
||||
await materializeModerationStateWorkingView({
|
||||
state,
|
||||
workingPath: stateWorkingPath,
|
||||
});
|
||||
|
||||
const strongestShifts = touchedVectors
|
||||
.sort((left, right) => Math.abs(right.delta) - Math.abs(left.delta))
|
||||
|
|
|
|||
Loading…
Reference in a new issue