refactor(modules): extract scheduling as registry-based module

Moves the scheduling surface — 5 delivery actions (schedule_task,
cancel_task, pause_task, resume_task, update_task), handleRecurrence,
applyPreTaskScripts, and task DB helpers — out of core and into
src/modules/scheduling/ (host) and container/agent-runner/src/scheduling/
(container).

First PR to fill the MODULE-HOOK markers introduced in PR #2:
  - src/host-sweep.ts MODULE-HOOK:scheduling-recurrence now dynamically
    imports handleRecurrence from the module each sweep tick.
  - container/agent-runner/src/poll-loop.ts MODULE-HOOK:scheduling-pre-task
    dynamically imports applyPreTaskScripts before the provider call.
    When the marker block is empty (scheduling uninstalled), `keep`
    falls back to `normalMessages` so non-task messages still flow.

The 5 task cases are removed from delivery.ts's handleSystemAction
switch — the registry now routes them. Task DB helpers moved out of
src/db/session-db.ts (which kept `nextEvenSeq` as a named export so
the module can uphold the host-writes-even-seq invariant). Test suite
split to match: scheduling-specific tests live in the module.

No migration — tasks are messages_in rows with kind='task'.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
gavrielc
2026-04-18 16:17:47 +03:00
parent 71aab8c316
commit 473f766585
12 changed files with 657 additions and 517 deletions

View File

@@ -0,0 +1,121 @@
import { execFile } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import type { MessageInRow } from '../db/messages-in.js';
import { touchHeartbeat } from '../db/connection.js';
const SCRIPT_TIMEOUT_MS = 30_000;
const SCRIPT_MAX_BUFFER = 1024 * 1024;
export interface ScriptResult {
wakeAgent: boolean;
data?: unknown;
}
function log(msg: string): void {
console.error(`[task-script] ${msg}`);
}
export async function runScript(script: string, taskId: string): Promise<ScriptResult | null> {
const scriptPath = path.join('/tmp', `task-script-${taskId}.sh`);
fs.writeFileSync(scriptPath, script, { mode: 0o755 });
return new Promise((resolve) => {
execFile(
'bash',
[scriptPath],
{ timeout: SCRIPT_TIMEOUT_MS, maxBuffer: SCRIPT_MAX_BUFFER, env: process.env },
(error, stdout, stderr) => {
try {
fs.unlinkSync(scriptPath);
} catch {
/* best-effort cleanup */
}
if (stderr) {
log(`[${taskId}] stderr: ${stderr.slice(0, 500)}`);
}
if (error) {
log(`[${taskId}] error: ${error.message}`);
return resolve(null);
}
const lines = stdout.trim().split('\n');
const lastLine = lines[lines.length - 1];
if (!lastLine) {
log(`[${taskId}] no output`);
return resolve(null);
}
try {
const result = JSON.parse(lastLine);
if (typeof result.wakeAgent !== 'boolean') {
log(`[${taskId}] output missing wakeAgent boolean: ${lastLine.slice(0, 200)}`);
return resolve(null);
}
resolve(result as ScriptResult);
} catch {
log(`[${taskId}] output is not valid JSON: ${lastLine.slice(0, 200)}`);
resolve(null);
}
},
);
});
}
export interface TaskScriptOutcome {
keep: MessageInRow[];
skipped: string[];
}
/**
* Run pre-task scripts for any task messages that carry one, serially.
* - Errors / missing output / wakeAgent=false → task id added to `skipped`.
* - wakeAgent=true → content JSON is mutated to carry `scriptOutput`, so the
* formatter renders it into the prompt.
* Non-task messages and tasks without scripts pass through unchanged.
*/
export async function applyPreTaskScripts(messages: MessageInRow[]): Promise<TaskScriptOutcome> {
const keep: MessageInRow[] = [];
const skipped: string[] = [];
for (const msg of messages) {
if (msg.kind !== 'task') {
keep.push(msg);
continue;
}
let content: Record<string, unknown>;
try {
content = JSON.parse(msg.content);
} catch {
keep.push(msg);
continue;
}
const script = typeof content.script === 'string' ? (content.script as string) : null;
if (!script) {
keep.push(msg);
continue;
}
log(`running script for task ${msg.id}`);
touchHeartbeat();
const result = await runScript(script, msg.id);
touchHeartbeat();
if (!result || !result.wakeAgent) {
const reason = result ? 'wakeAgent=false' : 'script error/no output';
log(`task ${msg.id} skipped: ${reason}`);
skipped.push(msg.id);
continue;
}
log(`task ${msg.id} wakeAgent=true, enriching prompt`);
content.scriptOutput = result.data ?? null;
keep.push({ ...msg, content: JSON.stringify(content) });
}
return { keep, skipped };
}