import { Worker } from 'node:worker_threads'; import { pathToFileURL } from 'node:url'; import type { ClawclawReminderManifest, ReminderInput, ReminderResult } from '../../../../sdk/perception-plugin/reminder.js'; import type { ResolvedClawclawPerceptionSource } from '../perception/perception-source.js'; import { runtimeModuleThreadLoaderConfig } from '../../../framework/modules/runtime-module-loader.js'; /** One bounded persistent Worker; module instances themselves are stateless between calls. */ export class ReminderRunner { private worker?: Worker; private sequence = 0; private pending?: { resolve: (value: any) => void; reject: (error: Error) => void; timer: ReturnType; id?: number }; private failure?: Error; constructor(readonly source: ResolvedClawclawPerceptionSource) { if (!source.reminder) throw new Error('Not a reminder perception source.'); } async start(): Promise<{ manifest: ClawclawReminderManifest; config: Record }> { if (this.source.compatibilityError) throw new Error(this.source.compatibilityError); const loader = runtimeModuleThreadLoaderConfig(); const promise = this.wait(8_000); this.worker = new Worker(new URL('../../../framework/modules/runtime-module-worker-bootstrap.mjs', import.meta.url), { execArgv: [], stdout: true, stderr: true, resourceLimits: { maxOldGenerationSizeMb: 64 }, workerData: { modulePath: this.source.sourcePath, expectedManifest: this.source.reminder, runtimeModuleBootstrap: { hostUrl: new URL('./reminder-worker.ts', import.meta.url).href, ...loader, esmModuleRoots: [new URL('.', pathToFileURL(this.source.sourcePath)).href] } }, }); this.worker.stdout?.on('data', () => {}); this.worker.stderr?.on('data', () => {}); this.worker.on('error', error => this.fail(error)); this.worker.on('exit', code => this.fail(new Error(`Reminder worker exited (${code}).`))); this.worker.on('message', message => { if (!this.pending || (message.id !== undefined && this.pending.id !== message.id)) return; if (message.type === 'error') { this.fail(new Error(message.error)); return; } clearTimeout(this.pending.timer); this.pending.resolve(message.type === 'ready' ? message : message.result); this.pending = undefined; }); return promise; } async update(input: ReminderInput): Promise { if (this.failure) throw this.failure; if (!this.worker) throw new Error('Reminder worker not started.'); const id = ++this.sequence; const promise = this.wait(500, id); this.worker.postMessage({ id, input }); return promise; } async stop(): Promise { await this.worker?.terminate(); this.worker = undefined; } private wait(timeout: number, id?: number): Promise { if (this.pending) throw new Error('Concurrent reminder invocation.'); return new Promise((resolve, reject) => { this.pending = { id, resolve, reject, timer: setTimeout(() => { this.fail(new Error('Reminder worker timeout.')); void this.stop(); }, timeout) }; }); } private fail(error: Error): void { this.failure = error; if (this.pending) { clearTimeout(this.pending.timer); this.pending.reject(error); this.pending = undefined; } } }