Files
nanoclaw/container/agent-runner/src/task-script.ts
exe.dev user b9f95df340 feat(v2): add pre-task script hook for scheduled tasks
Scheduled tasks can now carry a bash script that runs inside the container
before the agent is invoked. The script prints `{wakeAgent, data?}` on its
last stdout line; if `wakeAgent: false` (or the script errors) the task
row is marked completed and the agent is never queried, saving API calls
on no-op checks. On wake, the script's `data` is injected into the task
prompt. Semantics mirror V1: 30s bash timeout, 1MB buffer, last-line JSON,
error == skip.

Also blocks the Claude SDK's built-in scheduling tools (CronCreate,
CronDelete, CronList, ScheduleWakeup) via `disallowedTools` so tasks
actually flow through `mcp__nanoclaw__schedule_task` and get the script
gate. CLAUDE.md gains a soft pointer explaining why `schedule_task` is
the right path.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-16 16:31:29 +00:00

122 lines
3.4 KiB
TypeScript

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 };
}